想象一下,我有一个名为“国家”的数据框,其中有多列,包括国家及其各自的特质。
Country Idiosyncracies
Brazil We love football
France We love love
Italy We love Pizza
Italy We love Pizza
Italy We love Gellato
Brazil We love festivals
Italy We love Pizza
France We love bread
France We love bread
France We love bread
Italy We love Gellato
Italy We love Pizza
Brazil We love football
对于每个国家,我想将模式响应(idiosyncrasy)提取到另一个数据框中,如下所示
Country Top comment
Brazil We love football
France We love bread
Italy We love Pizza
这是我尝试过的代码
grouped = countries('Country')['Idiosyncracies'].mode()[0])
但是,我没有得到(3,2)数据帧,而是收到错误消息
TypeError: 'DataFrame' object is not callable
答案 0 :(得分:1)
使用GroupBy.apply
和Series.iat
来选择第一个值:
df = (countries.groupby('Country')['Idiosyncracies']
.apply(lambda x: x.mode().iat[0])
.reset_index())
print (df)
Country Idiosyncracies
0 Brazil We love football
1 France We love bread
2 Italy We love Pizza