I want to count the number of occurrences over two columns of a DataFrame :
No Name
1 A
1 A
5 T
9 V
Nan M
5 T
1 A
I expected df[["No", "Name"]].value_counts()
to give
No Name Count
1 A 3
5 T 2
9 V 1
Nan M 1
But I am missing the row containing NaN.
Is there a way to include NaNs in value_counts()?
You can still use value_counts() but with dropna=False rather than True (the default value), as follows:
df[["No", "Name"]].value_counts(dropna=False)
So, the result will be as follows:
No Name size
0 1 A 3
1 5 T 2
2 9 V 1
3 NaN M 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With