python - Pandas :如何找到每行最频繁的值?

标签 python pandas numpy dataframe

如何找到数据帧每一行中出现频率最高的值? 例如:

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

返回: [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

来自文档:

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

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])

关于python - Pandas :如何找到每行最频繁的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36091902/

相关文章:

Python 多索引函数,在分割列中保留顺序或其他可能的解决方案

python - Python中子数据帧中数据帧拆分的优化运行时间

python - 如何分组和更新 Pandas 的值(value)?

python - 如何更改包含数字的所有字符串单元格以在 pandas 中同时 float ?

python - sklearn-为什么这不能正确缩放?

python - 合并第 i^{th} 轴之前和之后的轴

python - 使用 Python 搜索字符串并将其与下一行一起写入新的文本文件

python - Sprite 没有向下 move

python - websocket-client 的 send() 尝试了多少次?

python - 如何操纵样本的 CDF 使其与不同样本的 CDF 相匹配?