简体   繁体   English

如何对 Pandas pivot 表格进行排序,但将总计保留在表格末尾

[英]How to sort a Pandas pivot table but keep totals at end of table

I built a pivot table doing this:我构建了一个 pivot 表来执行此操作:

prima.neta = df.pivot_table(index = ["seccion"], columns = "operacion", values = "prima_pesos", aggfunc = "sum", margins=True).fillna(0)

and then tried to sort the table in descending order by 'All' column (generated by margins=True ):然后尝试按“全部”列(由margins=True生成)降序对表进行排序:

prima.neta.sort_values(by='All', ascending=False)

This works fine, but the 'All' total (which of course is the highest amount) at the end of the original table output is brought up to the top as first line.这工作正常,但原始表 output 末尾的“全部”总数(当然是最高金额)作为第一行被带到顶部。

I would like to sort the table in descending order, but keep the 'All' (Totals) amounts at the last line.我想按降序对表格进行排序,但将“全部”(总计)金额保留在最后一行。

How can I achieve this?我怎样才能做到这一点?

Let's try this:让我们试试这个:

import pandas as pd
import numpy as np
np.random.seed(123)

# Create dummy dataframe
df = pd.DataFrame({'A':np.random.choice([*'ABC'], 36)
                  ,'B':np.random.choice([*'xyz'], 36)
                  ,'C':np.random.randint(0,100,36)})

# Pivot table with margins
results = df.pivot_table('C', 'A', 'B', aggfunc='sum', margins=True)

#Create temporary sortkey sort on sortkey and values, drop sortkey
result = results.assign(sortkey=results.index == 'All')\
                .sort_values(['sortkey','All'], ascending=[True, False])\
                .drop('sortkey', axis=1)
result

Output: Output:

B      x    y    z   All
A                       
B    368  215  275   858
A    155  202  218   575
C    206  149   45   400
All  729  566  538  1833

You can just swap the two rows.您可以交换两行。

ndf = df.reset_index()
totalind = ndf.index[ndf.All=='total'].tolist()[0]
ind = np.array(ndf.index)
ind[totaling], ind[-1] = ind.iloc[-1], ind.iloc[totalind]
ndf.reindex(ind)

There should be a less painful way, but I don't know what it is.应该有一个不那么痛苦的方法,但我不知道它是什么。

first you have to set_index column首先你必须 set_index 列

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

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