Python 按指定列获取平均值和模式组

Python 按指定列获取平均值和模式组,python,pandas,Python,Pandas,我想通过pandas获得毒性的模式编号(1或0)和毒性评分的平均值。我该怎么做? 谢谢。似乎您需要通过平均值进行聚合,并且: 备选方案是选择索引的第一个值: df = (df.groupby('rev_id', as_index=False) .agg({'toxicity_score':'mean', 'toxicity': lambda x: x.mode()})) 操作完成后,rev_id不再是一列。如何将结果转换为三列请检查上次编辑。mode()返回两个类似于[0,1]

我想通过pandas获得
毒性的模式编号(1或0)和
毒性评分的平均值。我该怎么做?
谢谢。

似乎您需要通过
平均值进行聚合,并且:

备选方案是选择索引的第一个值:

df = (df.groupby('rev_id', as_index=False)
        .agg({'toxicity_score':'mean', 'toxicity': lambda x: x.mode()}))

操作完成后,rev_id不再是一列。如何将结果转换为三列请检查上次编辑。mode()返回两个类似于[0,1]的数字。我只想通过rev_idIt获得最常见的数字组,您似乎需要
x.mode()[0]
或将pandas升级到最新版本,在
o.22.0
中,它运行良好。谢谢。如果一个rev_id中有五个1和五个0,那么在x.mode()中的顺序是什么?
df = (df.groupby('rev_id', as_index=False)
        .agg({'toxicity_score':'mean', 'toxicity': lambda x: x.mode()}))
df = (df.groupby('rev_id', as_index=False)
        .agg({'toxicity_score':'mean', 'toxicity': lambda x: x.value_counts().index[0]}))

print (df)
   rev_id  toxicity_score  toxicity
0  2232.0             0.4         0
1  4216.0             0.5         0
2  8953.0             0.1         0