基于另一列的数据框中列的模式
mode of a column in a dataframe based on another column
假设我有一个名为 'countries' 的数据框,其中包含多个列,包括国家及其各自的特质。
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
对于每个国家,我想将模式响应(特质)提取到另一个数据框中,如下所示
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
使用 GroupBy.apply
with Series.iat
作为 select 第一个值:
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
假设我有一个名为 'countries' 的数据框,其中包含多个列,包括国家及其各自的特质。
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
对于每个国家,我想将模式响应(特质)提取到另一个数据框中,如下所示
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
使用 GroupBy.apply
with Series.iat
作为 select 第一个值:
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