我有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
使用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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句