【发布时间】:2022-11-30 15:40:00
【问题描述】:
具有如下数据框:
我需要在每一行中找到最大值并返回列名。
预期输出:
我尝试了以下代码:
df['Max'] = df.idxmax(axis=1)
error:TypeError: reduction operation 'argmax' not allowed for this dtype
【问题讨论】:
具有如下数据框:
我需要在每一行中找到最大值并返回列名。
预期输出:
我尝试了以下代码:
df['Max'] = df.idxmax(axis=1)
error:TypeError: reduction operation 'argmax' not allowed for this dtype
【问题讨论】:
似乎添加 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
【讨论】: