-1

As an example, my code is follows

df = df.groupby('a').agg({'b': 'nunique', 'c': 'nunique', 'd': 'sum'}).reset_index()

I am trying to count the distinct number of entries of column b and column c, which are both text columns. Using nunique does not work so I essentially need to replace nunique.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Kishan
  • 1
  • Welcome to Stack Overflow! Please take the [tour]. Please provide some example data, your expected output, and for completeness, the actual output. See [How to make good reproducible pandas examples](/q/20109391/4518341). More broadly, see [mre], as well as [ask]. You can [edit] your post. – wjandrea Jul 01 '23 at 17:57
  • I made up some data and tried your code and it worked fine, so I'm not sure what the problem could be. – wjandrea Jul 01 '23 at 18:03

1 Answers1

0

Hellos you can use a Dataframe to modiyfy the columns and value_counts object to get the counts of text into the columns

import pandas as pd
    
r1=['b','c','d']
    
df=pd.DataFrame(r1,columns=['c'])
    
print(df)
print(df.c.apply(lambda x: pd.value_counts(x.split( ))).sum(axis = 0))

   c
0  b
1  c
2  d
3  c
4  d
b    1.0
c    2.0
d    2.0
dtype: float64