简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM