【问题标题】:Maximum of Each Row and return the columname in a dataframe每行的最大值并返回数据框中的列名
【发布时间】:2022-11-30 15:40:00
【问题描述】:

具有如下数据框:

我需要在每一行中找到最大值并返回列名。

预期输出:

我尝试了以下代码:

df['Max'] = df.idxmax(axis=1)
error:TypeError: reduction operation 'argmax' not allowed for this dtype

【问题讨论】:

    标签: python dataframe


    【解决方案1】:

    似乎添加 numeric_only 标志可以解决您的问题:

    >>> import pandas as pd
    .... # Create data frame
    >>> df
       Product  Price1  Price2  Price3     max
    0  Apple12      40      30      10  price1
    1    Grape      10      12      13  price3
    2   Orange       5       5       2  price4
    >>> df.idxmax(axis=1, numeric_only=True)
    0    Price1
    1    Price3
    2    Price1
    

    【讨论】:

    • df['Max']=df.idxmax(axis=1,numeric_only=True),我尝试了代码。
    • 错误:TypeError Traceback(最近调用最后一次)输入 In [361],在 <cell line: 1>() ----> 1 df['Max']=df.idxmax(axis=1,numeric_only=True) TypeError: idxmax() 得到了一个意外的关键字参数 'numeric_only'
    猜你喜欢
    • 2021-10-13
    • 2021-11-04
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多