【发布时间】:2021-04-06 23:24:14
【问题描述】:
您能帮我按多个条件对熊猫数据框进行分组吗?
这是我在 SQL 中的操作方式:
with a as (
select high
,sum( case when qr = 1 and now = 1 then 1 else 0 end ) q1_bad
,sum( case when qr = 2 and now = 1 then 1 else 0 end ) q2_bad
from #tmp2
group by high
)
select a.high from a
where q1_bad >= 2 and q2_bad >= 2 and a.high is not null
这是数据集的一部分:
import pandas as pd
a = pd.DataFrame()
a['client'] = range(35)
a['high'] = ['02','47','47','47','79','01','43','56','46','47','17','58','42','90','47','86','41','56',
'55','49','47','49','95','23','46','47','80','80','41','49','46','49','56','46','31']
a['qr'] = ['1','1','1','1','2','1','1','2','2','1','1','2','2',
'2','1','1','1','2','1','2','1','2','2','1','1','1','2','2','1','1',
'1','1','1','1','2']
a['now'] = ['0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0',
'0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0']
非常感谢!
【问题讨论】:
-
为你做了一点谷歌搜索:stackoverflow.com/questions/17266129/…
-
jeffery_the_wind,非常感谢)
标签: python-3.x pandas dataframe