【发布时间】:2016-04-08 00:41:46
【问题描述】:
我使用另一个 Dataframe 创建了一个带有 MultiIndex 的 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)
如文档http://pandas.pydata.org/pandas-docs/stable/advanced.html 中所述,这工作正常。
在文档中,它还说需要对标签进行排序,以便在“使用 MultiIndex 进行排序”下的索引和切片功能的正确工作。
但不知何故
multindexed_df.sort_index(level=0)
或
multindexed_df.sort_index(level='bus_uid')
不再起作用并抛出 TypeError: sort_index() got an unexpected keyword argument 'level'。
在 sort_index() 上查找对象信息,它看起来像“by”是我的新朋友,而不是“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.
我的问题如下:如何对 MultiIndex 进行排序,以便所有功能(切片等)都能正常工作?
【问题讨论】: