Create multiindex from existing dataframe
You could simply use groupby
in this case, which will create the multi-index automatically when it sums the sales along the requested columns.
df.groupby(['user_id', 'account_num', 'dates']).sales.sum().to_frame()
You should also be able to simply do this:
df.set_index(['user_id', 'account_num', 'dates'])
Although you probably want to avoid any duplicates (e.g. two or more rows with identical user_id
, account_num
and date
values but different sales figures) by summing them, which is why I recommended using groupby
.
If you need the multi-index, you can simply access viat new_df.index
where new_df
is the new dataframe created from either of the two operations above.
And user_id
will be level 0 and account_num
will be level 1.
For clarification of future users I would like to add the following:
As said by Alexander,
df.set_index(['user_id', 'account_num', 'dates'])
with a possible inplace=True
does the job.
The type(df)
gives
pandas.core.frame.DataFrame
whereas type(df.index)
is indeed the expected
pandas.core.indexes.multi.MultiIndex