如何使用值>计算行的大小和数量2用于以下数据框:
a
,输出应为
df
kpi_date cell_name call_drop
2016-01-01 bgl_2345 0.2
2016-01-01 bgl_2346 2.3
2016-01-01 blg_2347 0.3
2016-01-02 bgl_2345 1.4
2016-01-02 bgl_2346 2.5
2016-01-03 bgl_2347 2.7
请指导
答案 0 :(得分:0)
逐步进行:
grouped = df.groupby('kpi_date')
count = grouped['call_drop'].count()
greater2 = grouped.apply(lambda x: x['call_drop'][x['call_drop']> 2].count())
greater2.name = 'call_Drop>2'
print(pd.concat([count, greater2], axis=1))
输出:
call_drop call_Drop>2
kpi_date
2016-01-01 3 1
2016-01-02 2 1
2016-01-03 1 1
答案 1 :(得分:0)
您可以参考Returning a Series to propagate names,其中包含以下代码:
gd = df.groupby('kpi_date')
print(gd['call_drop'].apply(lambda x: {'call_drop':x.count(), 'call_drop>2':x[x>2].count()}).unstack())
Output:
call_drop call_drop>2
kpi_date
1/1/2016 3 1
2/1/2016 2 1
3/1/2016 1 1