使用python将另一列的列和总和内容分组

时间:2016-08-24 07:16:02

标签: python pandas dataframe group-by aggregate

我有一个数据框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_FxACT_TIME_AERATEUR_1_F1ACT_TIME_AERATEUR_1_F3ACT_TIME_AERATEUR_1_F5)创建一个包含以下列的数据框:class_energy和{{1 }}

例如,对应于sum_time的数据框:

ACT_TIME_AERATEUR_1_F1

我要做的事情我应该像这样使用该组:

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

有什么好主意帮我吗?

1 个答案:

答案 0 :(得分:7)

您可以将所有列添加到[]以进行汇总:

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