【发布时间】:2022-07-07 17:38:13
【问题描述】:
我有一个数据框,
df = pd.DataFrame({'a':[12,34,98,26],'b':[12,87,98,12],'c':[11,23,43,1]})
a b c
0 12 12 11
1 34 87 23
2 98 98 43
3 26 12 1
我想制作一个包含布尔值的 max_df。在 df 中,如果行中的条目是其行的最大值,则将有“True”代替 max_df 中的该条目,否则将有“False”。 我的 max_df 应该是这样的,
a b c
0 True True False
1 False True False
2 True True False
3 True False False
我为此编写了这段代码,
max_df = df.eq(df.max(axis=1), axis=0)
但它给出了值错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
有什么办法吗?
【问题讨论】:
-
您的解决方案对我有用,您使用的是哪个版本的 pandas?
-
我在 ubuntu 上运行,它有旧版本的 python,在它上面不起作用。不幸的是,我无法升级版本,因为我没有权限。有其他解决方案吗?