按单列搜索具有分层索引的 Pandas 数据框
Search a Pandas Dataframe having hierarchical indexing by a single column
我正在使用这个数据框:
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
哪个returns
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()
returns 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
我正在使用这个数据框:
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
哪个returns
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()
returns 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