pandas :如何找到每行最频繁的价值? [英] pandas: how to find the most frequent value of each row?

查看:77
本文介绍了 pandas :如何找到每行最频繁的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找数据帧每一行的最频繁值? 例如:

how to find the most frequent value of each row of a dataframe? For example:

In [14]: df
Out[14]:
   a  b  c
0  2  3  3
1  1  1  2
2  7  7  8

返回: [3,1,7]

return: [3,1,7]

推荐答案

尝试.mode()方法:

In [88]: df
Out[88]:
   a  b  c
0  2  3  3
1  1  1  2
2  7  7  8

In [89]: df.mode(axis=1)
Out[89]:
   0
0  3
1  1
2  7

来自文档:

获取沿选定轴的每个元素的模式.添加一行 对于每个标签的每种模式,用nan填充空白.

Gets the mode(s) of each element along the axis selected. Adds a row for each mode per label, fills in gaps with nan.

注意,可能为所选项返回多个值 轴(当一项以上共享最大频率时),即 返回数据框的原因.如果你想归咎于失踪 使用数据帧df中的模式的值,您可以执行以下操作: df.fillna(df.mode().iloc [0])

Note that there could be multiple values returned for the selected axis (when more than one item share the maximum frequency), which is the reason why a dataframe is returned. If you want to impute missing values with the mode in a dataframe df, you can just do this: df.fillna(df.mode().iloc[0])

这篇关于 pandas :如何找到每行最频繁的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆