我有这样的df
States Counts
AK one
AK two
AK one
LO one
LO three
LO three
试图获取每种状态下出现次数最多的计数
我的代码:
df.groupby('States')['Counts'].value_counts().first(), gives
TypeError: first() missing 1 required positional argument: 'offset'
预期输出:
States Counts
AK one
LO three
答案 0 :(得分:3)
使用lambda
函数:
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