Q:

how to groupby and calculate the percentage of non missing values in each column in pandas?

I have the following 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 

i need to group by var column and find the percentage of non missing value in loyal_date column for each group. Is there any way to do it using lambda function?    

A:

try this:
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

