简体   繁体   English

如何使用 Pandas 从多列中确定最大值

[英]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.

相关问题 如何将多列中的最大值返回到 pandas df 中的新列 - How to return the highest value from multiple columns to a new column in a pandas df 在 pandas 中使用 for 循环从两列中附加最高值的列表 - Appending list with highest value from two columns using for loop in pandas 如何确定同一行(Python,SQLite3)中三列的最大值 - How do I determine the highest value of three columns from the same row, Python, SQLite3 Pandas 如何从一列创建重复列表,并且只保留对应列的最大值? - Pandas How do I create a list of duplicates from one column, and only keep the highest value for the corresponding columns? 如何从许多列中获取最高值,并显示在使用熊猫的行中显示了什么? - How to get the highest values from many columns and show in what rows it happened using pandas? 如何使用 Pandas 将值从多列传输到其他列? - How to transfer values from multiple columns to other columns using Pandas? 使用上一行的两列来确定熊猫数据框中的列值 - Using two columns from previous row to determine column value in a pandas data frame 如何使用 Pandas 查找多列的行值 - How to find row value for multiple columns using Pandas 如何从熊猫中的字典值创建多行和多列 - how to create multiple rows and columns from a dictionary value in pandas 如何确定每行多列中类别标签的最高出现次数 - How to determine highest occurrence of categorical labels across multiple columns per row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM