How to get first value in groupby , TypeError: first() missing 1 required positional argument: 'offset'

Viewed 10942

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
1 Answers

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
Related