如何分组并计算熊猫每列中非缺失值的百分比?

时间:2017-03-19 22:45:04

标签: python pandas dataframe

我有以下datadrame

var     loyal_date
 1      2017-01-17     
 1      2017-01-03     
 1      2017-01-11     
 1       NaT           
 1       NaT            
 2      2017-01-15     
 2      2017-01-07      
 2      Nat      
 2      Nat      
 2      Nat 

我需要按var列分组,并在每个组的loyal_date列中找到非缺失值的百分比。有没有办法用lambda函数做到这一点?

1 个答案:

答案 0 :(得分:2)

试试这个:

In [59]: df
Out[59]:
   var loyal_date
0    1 2017-01-17
1    1 2017-01-03
2    1 2017-01-11
3    1        NaT
4    1        NaT
5    2 2017-01-15
6    2 2017-01-07
7    2        NaT
8    2        NaT
9    2        NaT

In [60]: df.groupby('var')['loyal_date'].apply(lambda x: x.notnull().sum()/len(x)*100)
Out[60]:
var
1    60.0
2    40.0
Name: loyal_date, dtype: float64