[英]How to determine the highest value from multiple columns using Pandas
The code below is example of how I have been attempting to find out what the highest value from multiple columns is, and then putting the highest value into a newly created column, using pandas.下面的代码是我如何尝试找出多列中的最大值,然后使用 pandas 将最大值放入新创建的列的示例。
def Alphabet (row):
AlpFields = ["A", "B", "C", "D", "E", "F"]
Alp = len(AlpFields)
AlpCheck = row[:Alp]
MaxValue = max(AlpCheck)
if MaxValue == 0:
return MaxValue, ""
return MaxValue , AlpFields[AlpCheck.index(MaxValue)]
df.apply(lambda row: Alphabet(row), axis =1)
df['HighestAlphabetScore'] = df.apply(lambda row: Alphabet(row), axis =1)
In this case I am trying to find what the highest value across columns: A, B, C, D, E and F is and then placing this value in the newly created column "Highest Alphabet Score".在这种情况下,我试图找出列中的最高值:A、B、C、D、E 和 F,然后将此值放在新创建的列“最高字母分数”中。
However when I run the code the following error comes up:但是,当我运行代码时,会出现以下错误:
TypeError: ("'Index' object is not callable", u'occurred at index 0')
TypeError: ("'Index' object is not callable", u'发生在索引 0')
I have tried a variety of different things to try and fix the issue, but so far nothing has worked.我尝试了各种不同的方法来尝试解决这个问题,但到目前为止没有任何效果。 Any help on what I am possibly doing wrong/any suggestions on how to get around this error would be really appreciated.
对我可能做错的任何帮助/关于如何解决此错误的任何建议将不胜感激。
For your dataframe df
you can call the max
function for the column axis and assign the result to a new column.对于您的 dataframe
df
,您可以为列轴调用max
function 并将结果分配给新列。
Say that you need the maximum among only specific columns, then the code would be假设您只需要特定列中的最大值,那么代码将是
df['HighestAlphabetScore'] = df[["A", "B", "C", "D", "E", "F"]].max(axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.