Reputation: 1097
I have a dataframe
Date Name
1995 Harry
1995 John
1997 NaN
1995 NaN
1998 Nina
1998 NaN
1997 Carrie
I need to count a NaN/(not NaN) values for each Date. So the output should be
Date Nan/NaN+notNaN
1995 1/3
1997 1/2
1998 1/2
I was trying with
df.groupby(['Date']).agg({'Name' : 'count'})
but can i do the same with
df.groupby(['Date']).agg({'df.Name.isnull()' : 'count'})
or smth like that?
Upvotes: 1
Views: 2233
Reputation: 31662
What about something like that:
In [52]: df.groupby('Date').agg({'Name': lambda x: x.isnull().sum(), 'Date': 'count'})
Out[52]:
Name Date
Date
1995 1 3
1997 1 2
1998 1 2
Or you could do following:
In [60]: df.groupby('Date').agg({'Name': lambda x: x.isnull().sum().astype(str) + '/' + str(x.size)})
Out[60]:
Name
Date
1995 1/3
1997 1/2
1998 1/2
Or with format
:
In [62]: df.groupby('Date').agg({'Name': lambda x: '{}/{}'.format(x.isnull().sum(), len(x))})
Out[62]:
Name
Date
1995 1/3
1997 1/2
1998 1/2
Upvotes: 3