熊猫按指定列获取平均值和众数组

亚纳琴
     rev_id  worker_id  toxicity  toxicity_score
0    2232.0        723         0             0.0
1    2232.0       4000         0             0.0
2    2232.0       3989         0             1.0
3    2232.0       3341         0             0.0
4    2232.0       1574         0             1.0
5    2232.0       1508         0             1.0
6    2232.0        772         0             1.0
7    2232.0        680         0             0.0
8    2232.0        405         0             1.0
9    2232.0       4020         1            -1.0
10   4216.0        500         0             0.0
11   4216.0        599         0             0.0
12   4216.0        339         0             2.0
13   4216.0        257         0             0.0
14   4216.0        303         0             1.0
15   4216.0        188         0             0.0
16   4216.0       1549         0             1.0
17   4216.0         64         0             1.0
18   4216.0       1527         0             0.0
19   4216.0       1502         0             0.0
20   8953.0       2596         0             1.0
21   8953.0       2403         0             0.0
22   8953.0       2539         0             0.0
23   8953.0       2542         0             0.0
24   8953.0       2544         0             0.0
25   8953.0       1016         0             0.0
26   8953.0       2550         0             0.0
27   8953.0       2578         0             0.0
28   8953.0       2494         0             0.0
29   8953.0        971         0             0.0

我想通过熊猫通过rev_idtoxicitytoxicity_score组中获取模式编号(1或0)以及平均值我怎样才能做到这一点 ?谢谢。

耶斯列尔

似乎您需要groupby通过进行汇总agg meanmode

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

替代方法是value_counts选择索引的第一个值:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章