Pass arguments to function while using apply to pandas series

Question:

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'

Asked By: Leo

||

Answers:

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
Answered By: jezrael
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.