【问题标题】:How to use Groupby with condition in Python如何在 Python 中使用带有条件的 Groupby
【发布时间】:2017-01-06 10:39:54
【问题描述】:

我有一个名为 merge_df_energy 的数据框

merged_df_energy.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 11232 entries, 0 to 11231
Data columns (total 17 columns):
TIMESTAMP                       11232 non-null datetime64[ns]
P_ACT_KW                        11232 non-null int64
PERIODE_TARIF                   11232 non-null object
P_SOUSCR                        11232 non-null int64
high_energy                     11232 non-null int64
medium_energy                   11232 non-null int64
low_energy                      11232 non-null int64
0ACT_TIME_ETA_PRG_P2REF_RM      11232 non-null int64
0ACT_TIME_ETA_PRG_VDES_RM       11232 non-null int64
0ACT_TIME_ETA_PRG_P3REF_RM      11232 non-null int64
0ACT_TIME_ETA_POMP_RECIRC_N1    11232 non-null int64
0ACT_TIME_ETA_POMP_RECIRC_N2    11232 non-null int64
0ACT_TIME_ETA_POMP_RECIRC_N3    11232 non-null int64
0ACT_TIME_ETA_SURPRES_AIR_N1    11232 non-null int64
0ACT_TIME_ETA_SURPRES_AIR_N2    11232 non-null int64
0ACT_TIME_ETA_SURPRES_AIR_N3    11232 non-null int64
class_energy                    11232 non-null object
dtypes: datetime64[ns](1), int64(14), object(2)
memory usage: 1.5+ MB

采用这种结构:

TIMESTAMP P_ACT_KW PERIODE_TARIF P_SOUSCR high_energy medium_energy low_energy 0ACT_TIME_ETA_PRG_P2REF_RM 0ACT_TIME_ETA_PRG_VDES_RM 
0ACT_TIME_ETA_PRG_P3REF_RM 0ACT_TIME_ETA_POMP_RECIRC_N1 0ACT_TIME_ETA_POMP_RECIRC_N2 0ACT_TIME_ETA_POMP_RECIRC_N3 
0ACT_TIME_ETA_SURPRES_AIR_N1 0ACT_TIME_ETA_SURPRES_AIR_N2 
0ACT_TIME_ETA_SURPRES_AIR_N3 class_energy


2016-05-10 04:30:00 107 HP 250 107 0 0 100 0 0 0 0 0 0 0 0 high 

2016-05-10 04:40:00 109 HC 250 109 0 0 0 0 100 0 0 0 0 0 0 high 

2016-05-10 04:50:00 106 HP 250 106 0 0 0 0 100 0 0 0 0 0 0 high

我试图通过(class_energy)来计算(0ACT_TIME_ETA_PRG_P2REF_RM,0ACT_TIME_ETA_PRG_VDES_RM,0ACT_TIME_ETA_PRG_P3REF_RM,0ACT_TIME_ETA_POMP_RECIRC_N1 0ACT_TIME_ETA_POMP_RECIRC_N2,0ACT_TIME_ETA_POMP_RECIRC_N3,0ACT_TIME_ETA_SURPRES_AIR_N1,0ACT_TIME_ETA_SURPRES_AIR_N2,0ACT_TIME_ETA_SURPRES_AIR_N3 class_energy)基团的总和。 P>

为此我做到了:

df_F1 = (merged_df_energy.groupby(by=['class_energy'], as_index=False)['0ACT_TIME_ETA_PRG_P2REF_RM', '0ACT_TIME_ETA_PRG_VDES_RM','0ACT_TIME_ETA_PRG_P3REF_RM','0ACT_TIME_ETA_POMP_RECIRC_N1','0ACT_TIME_ETA_POMP_RECIRC_N2', '0ACT_TIME_ETA_POMP_RECIRC_N3', '0ACT_TIME_ETA_SURPRES_AIR_N1', '0ACT_TIME_ETA_SURPRES_AIR_N2', '0ACT_TIME_ETA_SURPRES_AIR_N3' ].sum())

它工作正常,但我想知道如何在这种情况下做到这一点(如果 PERIODE_TARIF = 'HP')?

【问题讨论】:

    标签: python pandas dataframe group-by conditional-statements


    【解决方案1】:

    我想你之前需要groupbyboolean indexing:

    merged_df_energy1 = merged_df_energy[merged_df_energy.PERIODE_TARIF == 'HP']
    
    cols = ['0ACT_TIME_ETA_PRG_P2REF_RM', 
           '0ACT_TIME_ETA_PRG_VDES_RM',
           '0ACT_TIME_ETA_PRG_P3REF_RM',
           '0ACT_TIME_ETA_POMP_RECIRC_N1',
           '0ACT_TIME_ETA_POMP_RECIRC_N2', 
           '0ACT_TIME_ETA_POMP_RECIRC_N3', 
           '0ACT_TIME_ETA_SURPRES_AIR_N1', 
           '0ACT_TIME_ETA_SURPRES_AIR_N2', 
           '0ACT_TIME_ETA_SURPRES_AIR_N3']
    df_F1 = (merged_df_energy1.groupby(by=['class_energy'], as_index=False)[cols].sum())
    
    print (df_F1)
      class_energy  0ACT_TIME_ETA_PRG_P2REF_RM  0ACT_TIME_ETA_PRG_VDES_RM  \
    0         high                         100                          0   
    
       0ACT_TIME_ETA_PRG_P3REF_RM  0ACT_TIME_ETA_POMP_RECIRC_N1  \
    0                         100                             0   
    
       0ACT_TIME_ETA_POMP_RECIRC_N2  0ACT_TIME_ETA_POMP_RECIRC_N3  \
    0                             0                             0   
    
       0ACT_TIME_ETA_SURPRES_AIR_N1  0ACT_TIME_ETA_SURPRES_AIR_N2  \
    0                             0                             0   
    
       0ACT_TIME_ETA_SURPRES_AIR_N3  
    0                             0  
    

    编辑:

    如果列的顺序从未改变,您可以使用:

    cols = merged_df_energy.columns[7:16]
    print (cols)
    Index(['0ACT_TIME_ETA_PRG_P2REF_RM', '0ACT_TIME_ETA_PRG_VDES_RM',
           '0ACT_TIME_ETA_PRG_P3REF_RM', '0ACT_TIME_ETA_POMP_RECIRC_N1',
           '0ACT_TIME_ETA_POMP_RECIRC_N2', '0ACT_TIME_ETA_POMP_RECIRC_N3',
           '0ACT_TIME_ETA_SURPRES_AIR_N1', '0ACT_TIME_ETA_SURPRES_AIR_N2',
           '0ACT_TIME_ETA_SURPRES_AIR_N3'],
          dtype='object')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      • 2016-03-08
      • 1970-01-01
      • 2020-08-13
      相关资源
      最近更新 更多