5

I have df, like this

States Counts
AK     one
AK     two
AK     one
LO     one
LO     three
LO     three

trying to get most occured counts for each status

my code:

 df.groupby('States')['Counts'].value_counts().first(), gives 

TypeError: first() missing 1 required positional argument: 'offset'

expected output:

  States Counts
  AK     one
  LO     three
Pyd
  • 6,017
  • 18
  • 52
  • 109

1 Answers1

5

Use lambda function:

df = df.groupby('States')['Counts'].apply(lambda x: x.value_counts().index[0]).reset_index(name='val')
print (df)
  States    val
0     AK    one
1     LO  three
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252