1

I'm trying to create a new Dataframe with a multi index. I load a list with the dates for the first level and have a list of patients for the second level. I can't grasp the method for constructing the proper Dataframe structure... Basic code.

factor_index = pd.date_range(start=startDate, end=endDate)
factor_index.name = 'Date'
factorData = pd.DataFrame(index=factor_index)

this creates the frame with the dates as the primary index... I need to set a second index for each of the dates that lists the patients under each date. I have a list of patients. It seems like there's a one-liner for this but I can't seem to find it in my searching.

1 Answer 1

1

Use MultiIndex.from_product:

list_of_patient = ['a','b','c']
factor_index = pd.date_range(start=startDate, end=endDate)
mux = pd.MultiIndex.from_product([factor_index, list_of_patient], names=('Date','Patient'))
factorData = pd.DataFrame(index=mux)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.