2016-01-09 25 views
3

如何與值來計算行的大小和計數與所述計數> 2以下的數據幀:獲取總的行和列值> 2使用GROUPBY

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 

和輸出應該是

kpi_date call_drop call_Drop>2 
    2016-01-01 3    1 
    2016-01-02 2    1 
    2016-01-03 1    1 

請指導

+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 
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