1

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?

0

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.