简体   繁体   English

如何创建具有多级列的数据透视表?

[英]How to create a pivot table with multiple level columns?

Here's my current code:这是我当前的代码:

pd.pivot_table(filtered_dfs[key], values=["Entered Debit Amount", "Entered Credit Amount", "Converted Debit Amount", "Converted Credit Amount"],
                    index=["ATTRIBUTE1 Value for Journal Entry Line DFF", "Segment3"], aggfunc=np.sum)

which works out fine and give me this output:效果很好,并给了我这个输出:

在此处输入图片说明

However I want the segment 3 to be a value to be grouped by as well, example:但是我希望第 3 段也是一个要分组的值,例如:

在此处输入图片说明

Is there a way to do this with pandas pivot table?有没有办法用熊猫数据透视表做到这一点?

IIUC use: IIUC 使用:

pd.pivot_table(filtered_dfs[key], values=["Entered Debit Amount", "Entered Credit Amount", "Converted Debit Amount", "Converted Credit Amount"],
                index=["ATTRIBUTE1 Value for Journal Entry Line DFF", "Segment3"], aggfunc=np.sum).unstack()

Or:或者:

pd.pivot_table(filtered_dfs[key], values=["Entered Debit Amount", "Entered Credit Amount", "Converted Debit Amount", "Converted Credit Amount"],
                index="ATTRIBUTE1 Value for Journal Entry Line DFF", columns="Segment3", aggfunc=np.sum)

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

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