如何过滤掉数据框中的行并求和

时间:2019-04-11 11:31:33

标签: pandas dataframe

我是熊猫新手 我有一个数据框,它有三列,值分别为

range()

我想根据如下所示的过滤器,按nTimeStamp求和

SQL代码

nTimeStamp       nMessageCode   nIOC
1544114048       12073           0
1544114048       12075           0         
1544114048       12073           1
1544114048       12075           1
1544114049       12074           0         
1544114049       12073           0
1544114049       12075           0

预期结果将是

SELect nTimeStamp, SUM(nFilter) nCount
 FROM (
Select nTimeStamp case when nMessageCode in (12073,12074)
                       then 1
                       when nMessageCode = 12075 and nIOC = 0
                       then 1
                       else 0
                   end 'nFilter'   
      )
 group by nTimeStamp

我如何实现相同的目标。请帮助

1 个答案:

答案 0 :(得分:0)

创建布尔掩码并用TrueSeries.groupby计数sum的值:

m = (df['nMessageCode'].isin((12073,12074)) | 
     (df['nMessageCode'].eq(12075) & df['nIOC'].eq(0)))

df1 = m.groupby(df['nTimeStamp']).sum().astype(int).reset_index(name='nCount')
print (df1)
   nTimeStamp  nCount
0  1544114048       3
1  1544114049       3