繁体   English   中英

按行和列的总和对熊猫数据透视表进行排序

[英]Sort pandas pivot table by sum of rows and columns

我有(例如)这个 DataFrame:

 COLUMN1 COLUMN2  VALUE
0    0102    1020      1
1    0102    1220      8
2    0102    1210      2
3    0103    1020      1
4    0103    1210      3
5    0103    1222      8
6    0104    1020      3
7    0104    1120      2

(实际上,它大约有 9000 行长。)

由此,我创建了数据透视表,其中索引为 COLUMN1,列为 COLUMN2,值来自 VALUES,由 0 填充,其中 NaN。

COLUMN2  1020  1120  1210  1220  1222
COLUMN1                              
0102        1     0     2     8     0
0103        1     0     3     0     8
0104        3     2     0     0     0

我必须先按总行数排序这个数据透视表,然后再按总列数排序。 那看起来像这样:

COLUMN2  1220  1222  1020  1210  1120| (GT)
COLUMN1                              |     HIGHEST
0103        0     8     1     3     0| (12) |
0102        8     0     1     2     0| (11) |
0104        0     0     3     0     2| (5)  V
--------------------------------------
(GT:        8     8     5     5     2)
 HIGHTEST---------------------------->  LOWEST

有没有办法做到这一点? 我尝试通过将索引和列作为列表导入来创建数据透视表,按照我希望它们出现的顺序排序,但是 Pandas 在创建表时似乎会自动对它们进行 AZ 排序。

示例代码:

import pandas as pd

exampledata=[['0102','1020',1],['0102','1220',8],['0102','1210',2],
             ['0103','1020',1],['0103','1210',3], ['0103','1222',8],
             ['0104','1020',3],['0104','1120',2]]

df = pd.DataFrame(exampledata,columns=['COLUMN1','COLUMN2','VALUE'])
print(df)
pivot = pd.pivot_table(df,
                       index='COLUMN1',
                       columns='COLUMN2',
                       values='VALUE',
                       aggfunc='sum',
                       fill_value=0)
print(pivot)

pivot_table有一个选项margin ,在这种情况下很方便:

(df.pivot_table(index='COLUMN1', columns='COLUMN2', values='VALUE',
               aggfunc='sum', fill_value=0, margins=True)   # pivot with margins 
   .sort_values('All', ascending=False)  # sort by row sum
   .drop('All', axis=1)                  # drop column `All`
   .sort_values('All', ascending=False, axis=1) # sort by column sum
   .drop('All')    # drop row `All`
)

输出:

COLUMN2  1220  1222  1020  1210  1120
COLUMN1                              
103         0     8     1     3     0
102         8     0     1     2     0
104         0     0     3     0     2

我会尝试这样的事情

pivot['sum_cols'] = pivot.sum(axis=1)
pivot = pivot.sort_values('sum_cols' , ascending=False)

您的数据透视表的索引(从值COLUMN1COLUMN2 )的类型为String ,并且排序String从A做Z.也许你应该输入索引Integer类型,则排序将数字进行。 考虑到pivot_table 文档columnsindex允许使用整数类型。

df = df.astype('int')

现在,您的pivot_table函数输出一个DataFrame ,您可以按照与任何DataFrame相同的方式按索引或按列对其进行DataFrame

根据sort_index 文档:要对索引进行排序,您应该执行以下操作:

pivot = pivot.sort_index(ascending=0)

要对列进行排序,您应该执行以下操作:

pivot = pivot.sort_index(axis=1, ascending=0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM