Home > OS >  Pass arguments to function while using apply to pandas series
Pass arguments to function while using apply to pandas series

Time:08-11

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'

CodePudding user response:

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
  • Related