Python Pandas为所选列的行最大值添加列

编程入门 行业动态 更新时间:2024-10-22 11:25:21
本文介绍了Python Pandas为所选列的行最大值添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 data = {'name' : ['bill', 'joe', 'steve'], 'test1' : [85, 75, 85], 'test2' : [35, 45, 83], 'test3' : [51, 61, 45]} frame = pd.DataFrame(data)

我想添加一个新列,以显示每一行的最大值.

I would like to add a new column that shows the max value for each row.

所需的输出:

name test1 test2 test3 HighScore bill 75 75 85 85 joe 35 45 83 83 steve 51 61 45 61

有时

frame['HighScore'] = max(data['test1'], data['test2'], data['test3'])

可以正常工作,但大多数情况下会出现此错误:

works but most of the time gives this error:

ValueError:具有多个元素的数组的真值不明确.使用a.any()或a.all()

为什么有时只能工作?还有另一种方法吗?

Why does it only work sometimes? Is there another way of doing it?

推荐答案

>>> frame['HighScore'] = frame[['test1','test2','test3']].max(axis=1) >>> frame name test1 test2 test3 HighScore 0 bill 85 35 51 85 1 joe 75 45 61 75 2 steve 85 83 45 85

更多推荐

Python Pandas为所选列的行最大值添加列

本文发布于:2023-10-27 03:06:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532129.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最大值   所选   Python   Pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!