4

鉴于此数据框和数据透视表:

import pandas as pd
df=pd.DataFrame({'A':['x','y','z','x','y','z'],
                 'B':['one','one','one','two','two','two'],
                 'C':[7,5,3,4,1,6]})
df


    A   B       C
0   x   one     7
1   y   one     5
2   z   one     3
3   x   two     4
4   y   two     1
5   z   two     6

table = pd.pivot_table(df, index=['A', 'B'],aggfunc=np.sum)

table
A  B  
x  one    7
   two    4
y  one    5
   two    1
z  one    3
   two    6
Name: C, dtype: int64

我想对数据透视表进行排序,使“A”的顺序为 z、x、y,而“B”的顺序基于数据框列“C”的降序排序值。

像这样:

A  B  
z  two    6
   one    3
x  one    7
   two    4
y  one    5
   two    1

    Name: C, dtype: int64

提前致谢!

4

3 回答 3

2

我不相信有一种简单的方法可以实现您的目标。以下解决方案首先根据 column 的值对表进行降序排序C。然后它根据您想要的顺序连接每个切片。

order = ['z', 'x', 'y']
table = table.reset_index().sort_values('C', ascending=False)
>>> pd.concat([table.loc[table.A == val, :].set_index(['A', 'B']) for val in order])
       C
A B     
z two  6
  one  3
x one  7
  two  4
y one  5
  two  1
于 2016-05-10T20:21:29.597 回答
2

如果您可以在 A 列中读取分类数据,那么它会变得更加简单。将您的类别设置为list('zxy')并指定ordered=True使用您的自定义排序。

您可以使用类似于以下内容的方式读取数据:

'A':pd.Categorical(['x','y','z','x','y','z'], list('zxy'), ordered=True)

或者,您可以照原样读入数据,然后用于astype将 A 转换为分类:

df['A'] = df['A'].astype('category', categories=list('zxy'), ordered=True)

一旦 A 是分类的,您可以像以前一样旋转,然后排序:

table = table.sort_values(ascending=False).sortlevel(0, sort_remaining=False)
于 2016-05-10T20:50:04.727 回答
1

解决方案

custom_order = ['z', 'x', 'y']
kwargs = dict(axis=0, level=0, drop_level=False)

new_table = pd.concat(
    [table.xs(idx_v, **kwargs).sort_values(ascending=False) for idx_v in custom_order]
)

交替一个班轮

pd.concat([table.xs(i, drop_level=0).sort_values(ascending=0) for i in list('zxy')]

解释

custom_order是您想要的订单。 kwargs是提高可读性的便捷方法(在我看来)。要注意的关键元素,如果您想进一步利用它,可能对您很重要axis=0level=0但是,这些也是默认值,可以省略。 drop_level=False是这里的关键论点,并且对于保持idx_v我们正在采取的一种xs方式是必要的,以便pd.concat以我们想要的方式将它们放在一起。

pd.concat我在通话中以与 Alexander 几乎完全相同的方式使用列表推导。

示范

print new_table

A  B  
z  two    6
   one    3
x  one    7
   two    4
y  one    5
   two    1
Name: C, dtype: int64
于 2016-05-10T20:21:37.863 回答