我已经使用以下方法对我的第一级索引进行了排序:Custom sort order function for groupby pandas python
def my_func(group):
return sum(group["B"]*group["C"])
idx=df.groupby('A').apply(my_func).reindex(df.index.get_level_values(0))
df.iloc[idx.argsort()]
问题是,在对第一级排序之后,第二级排序变得混乱。我怎样才能确保集团内部的订单得到维持?你知道吗
从
A B C
1 0 1 8
1 3 3
2 0 1 2
1 2 2
3 0 1 3
1 2 4
至
A B C
2 0 1 2
1 2 2
3 0 1 3
1 2 4
1 0 1 8
1 3 3
而不是(倒转最后两行)
A B C
2 0 1 2
1 2 2
3 0 1 3
1 2 4
1 1 3 3
0 1 8
我认为您需要稳定的排序algo-
mergesort
:相关问题 更多 >
编程相关推荐