I have created a Dataframe with a MultiIndex by using another Dataframe:
arrays = [df['bus_uid'], df['bus_type'], df['type'],
df['obj_uid'], df['datetime']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['bus_uid', 'bus_type', 'type',
'obj_uid', 'datetime'])
multindex_df = pd.DataFrame(df['val'].values, index=index)
This worked fine as described in the documentation http://pandas.pydata.org/pandas-docs/stable/advanced.html .
In the documentation it also says that the labels need to be sorted for the correct working of indexing and slicing functionalities under "The need for sortedness with MultiIndex".
But somehow
multindexed_df.sort_index(level=0)
or
multindexed_df.sort_index(level='bus_uid')
does not work anymore and throws TypeError: sort_index() got an unexpected keyword argument 'level'.
Looking up the object information on sort_index() it looks as "by" is my new friend instead of "levels":
by:object
Column name(s) in frame. Accepts a column name or a list for a nested sort. A tuple will be interpreted as the levels of a multi-index.
My question is the following: How can I sort my MultiIndex so that all functionalities (slicing,etc.) are working correctly?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…