这个问题在这里已有答案:
我有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