我正在使用此数据框:
import pandas as pd
df = pd.DataFrame([['A', 'one', 105], ['A', 'two', 101], ['A', 'three', 103],
['B','one', 101], ['B','two', 1102], ['B','three', 1050]],
columns=['c1', 'c2', 'c3'])
df = df.set_index(['c1', 'c2'])
df
哪个返回
c3
c1 c2
A one 105
two 101
three 103
B one 101
two 1102
three 1050
...我想按列c3排序,保留行和c1排序,以得到此信息:
c3
c1 c2
A one 105
three 103
two 101
B two 1102
three 1050
one 101
我无法提出一种不会混淆c1排序的方法。特别是,最后一个df.sort_index()
返回KeyError: 'c1'
您可以做的IIUC:
out = (df.sort_values(['c3','c1'],ascending=False)
.reindex(df.index.get_level_values(0).unique(),level=0))
c3
c1 c2
A one 105
three 103
two 101
B two 1102
three 1050
one 101
我想您可以使用:
df.sort_values(['c1','c3'], ascending=False).groupby(['c1','c3']).agg(lambda x: x)
输出:
c3
c1 c2
B two 1102
three 1050
one 101
A one 105
three 103
two 101