简体   繁体   English

汇总的Pandas DataFrames列集

[英]Aggregate sets of Pandas DataFrames columns

I have a pandas DataFrame with some independent columns, and I'm looking for an efficient way to unwind / aggregate them. 我有一个带有一些独立列的pandas DataFrame,我正在寻找一种有效的方法来展开/聚合它们。

So, let's say I have the table: 因此,假设我有桌子:

+-----+-----+-------+------+-------+
| One | Two | Three | Four | Count |
+-----+-----+-------+------+-------+
| a   | x   | y     | y    | 3     |
+-----+-----+-------+------+-------+
| b   | z   | x     | x    | 5     |
+-----+-----+-------+------+-------+
| c   | y   | x     | y    | 1     |
+-----+-----+-------+------+-------+

Where rows Two, Three and Four are independent. 其中第二,第三和第四行是独立的。

I would like to end up with the table: 我想结束这张桌子:

+-----+-------+-------+
| One | Other | Count |
+-----+-------+-------+
| a   | x     | 3     |
+-----+-------+-------+
| a   | y     | 6     |
+-----+-------+-------+
| b   | x     | 10    |
+-----+-------+-------+
| b   | z     | 5     |
+-----+-------+-------+
| c   | x     | 1     |
+-----+-------+-------+
| c   | y     | 2     |
+-----+-------+-------+

How would be the best way to achieve this? 如何做到这一点的最佳方法?

You can use melt function from pandas to reshape your data frame from wide to long format then groupby the One and Other columns and sum the Count column: 您可以使用pandas melt函数将数据框的格式从宽格式转换为长格式,然后对“ One和“ Other列进行分组,并对“ Countsum

import pandas as pd
pd.melt(df, id_vars = ['One', 'Count'], value_name = 'Other').groupby(['One', 'Other'])['Count'].sum().reset_index()

  One Other Count
0   a   x   3
1   a   y   6
2   b   x   10
3   b   z   5
4   c   x   1
5   c   y   2

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

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