【发布时间】:2021-10-03 15:54:57
【问题描述】:
以下是生成示例数据框的一些代码:
fruits=pd.DataFrame()
fruits['month']=['jan','feb','feb','march','jan','april','april','june','march','march','june','april']
ind_mnth=fruits['month'].values
fruits['fruit']=['apple','orange','pear','orange','apple','pear','cherry','pear','orange','cherry','apple','cherry']
ind_fruit=fruits['fruit'].values
fruits['price']=[30,20,40,25,30 ,45,60,45,25,55,37,60]
fruits_grp = fruits.set_index([ind_mnth, ind_fruit],drop=False)
如何对这个多索引数据框的行进行排序,使得每个外部索引(月)下的内部索引(水果)按照自定义顺序排序,具有相同外部索引的行组合在一起。
【问题讨论】:
标签: pandas dataframe multi-index