2

I want to pass an argument (dropna=False) to value_counts, when using apply with pandas dataframe:

columns = ['a','c']
df = pd.DataFrame({'a':[1,2,2,np.nan], 'b':[2,3,4,3], 'c': [4,np.nan,6,4]})
print (df.apply(pd.Series.value_counts)) #this works
print (df['a'].value_counts(dropna=False)) #this works
print (df.apply(pd.Series.value_counts(value_counts=False))) #combining doesn't 

OUT: 
       a    b    c
1.0  1.0  NaN  NaN
2.0  2.0  1.0  NaN
3.0  NaN  2.0  NaN
4.0  NaN  1.0  2.0
6.0  NaN  NaN  1.0
2.0    2
1.0    1
NaN    1
Name: a, dtype: int64
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [109], in <cell line: 5>()
      3 print (df.apply(pd.Series.value_counts))
      4 print (df['a'].value_counts(dropna=False))
----> 5 print (df.apply(pd.Series.value_counts(value_counts=False)))

TypeError: value_counts() got an unexpected keyword argument 'value_counts'

1 Answer 1

2

IIUC you need pass like argument dropna=False:

print (df.apply(pd.Series.value_counts, dropna=False))
       a    b    c
1.0  1.0  NaN  NaN
2.0  2.0  1.0  NaN
3.0  NaN  2.0  NaN
4.0  NaN  1.0  2.0
6.0  NaN  NaN  1.0
NaN  1.0  NaN  1.0

Or lambda function:

print (df.apply(lambda x: x.value_counts(dropna=False)))
       a    b    c
1.0  1.0  NaN  NaN
2.0  2.0  1.0  NaN
3.0  NaN  2.0  NaN
4.0  NaN  1.0  2.0
6.0  NaN  NaN  1.0
NaN  1.0  NaN  1.0
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.