按列分组并对另一列的内容求和

问题描述 投票:0回答:1

我有一个数据框

merged_df_energy

+------------------------+------------------------+------------------------+--------------+
| ACT_TIME_AERATEUR_1_F1 | ACT_TIME_AERATEUR_1_F3 | ACT_TIME_AERATEUR_1_F5 | class_energy |
+------------------------+------------------------+------------------------+--------------+
| 63.333333              | 63.333333              | 63.333333              | low          |
| 0                      | 0                      | 0                      | high         |
| 45.67                  | 0                      | 55.94                  | high         |
| 0                      | 0                      | 23.99                  | low          |
| 0                      | 20                     | 23.99                  | medium       |
+------------------------+------------------------+------------------------+--------------+

我想为每个

ACT_TIME_AERATEUR_1_Fx
ACT_TIME_AERATEUR_1_F1
ACT_TIME_AERATEUR_1_F3
ACT_TIME_AERATEUR_1_F5
)创建一个包含以下列的数据框:
class_energy
sum_time

例如对于

ACT_TIME_AERATEUR_1_F1
对应的数据框:

+-----------------+-----------+
|  class_energy   | sum_time  |
+-----------------+-----------+
| low             | 63.333333 |
| medium          | 0         |
| high            | 45.67     |
+-----------------+-----------+

我要做的就是像这样使用组:

data.groupby(by=['class_energy'])['sum_time'].sum()

我该怎么做?

python pandas dataframe group-by aggregate
1个回答
16
投票

您可以将所有列添加到

[]
进行聚合:

print (df.groupby(by=['class_energy'])['ACT_TIME_AERATEUR_1_F1', 'ACT_TIME_AERATEUR_1_F3','ACT_TIME_AERATEUR_1_F5'].sum())
              ACT_TIME_AERATEUR_1_F1  ACT_TIME_AERATEUR_1_F3  \
class_energy                                                   
high                       45.670000                0.000000   
low                        63.333333               63.333333   
medium                      0.000000               20.000000   

              ACT_TIME_AERATEUR_1_F5  
class_energy                          
high                       55.940000  
low                        87.323333  
medium                     23.990000  

您还可以使用参数

as_index=False
:

print (df.groupby(by=['class_energy'], as_index=False)['ACT_TIME_AERATEUR_1_F1', 'ACT_TIME_AERATEUR_1_F3','ACT_TIME_AERATEUR_1_F5'].sum())
  class_energy  ACT_TIME_AERATEUR_1_F1  ACT_TIME_AERATEUR_1_F3  \
0         high               45.670000                0.000000   
1          low               63.333333               63.333333   
2       medium                0.000000               20.000000   

   ACT_TIME_AERATEUR_1_F5  
0               55.940000  
1               87.323333  
2               23.990000  

如果只需要聚合前

3
列:

print (df.groupby(by=['class_energy'], as_index=False)[df.columns[:3]].sum())
  class_energy  ACT_TIME_AERATEUR_1_F1  ACT_TIME_AERATEUR_1_F3  \
0         high               45.670000                0.000000   
1          low               63.333333               63.333333   
2       medium                0.000000               20.000000   

   ACT_TIME_AERATEUR_1_F5  
0               55.940000  
1               87.323333  
2               23.990000  

...或没有最后一个的所有列:

print (df.groupby(by=['class_energy'], as_index=False)[df.columns[:-1]].sum())
  class_energy  ACT_TIME_AERATEUR_1_F1  ACT_TIME_AERATEUR_1_F3  \
0         high               45.670000                0.000000   
1          low               63.333333               63.333333   
2       medium                0.000000               20.000000   

   ACT_TIME_AERATEUR_1_F5  
0               55.940000  
1               87.323333  
2               23.990000  
© www.soinside.com 2019 - 2024. All rights reserved.