我有3个变量,分别称为a,b和c,它们是float64(5000,)。
我想要每一行的最大值。
a b c
3 4 2
5 5 4
3 7 8
我想要的结果
4
5
8
所以我认为我可以使用下面的行,
myResult = max(a,b,c)
但收到此错误消息
ValueError:具有多个元素的数组的真值不明确。使用a.any()或a.all()
如何获得理想的结果?
答案 0 :(得分:1)
您可以使用pandas.DataFrame.apply
将每一行映射到该行的pandas.Series.max
:
print(df.apply(pd.Series.max, axis=1))
输出:
0 4
1 5
2 8
dtype: int64
答案 1 :(得分:1)
如果您使用的是numpy,则可以使用stack和max来实现:
>>> import numpy as np
>>> a = np.array((3, 5, 3))
>>> b = np.array((4, 5, 7))
>>> c = np.array((2, 4, 8))
>>> stacked_matrices = np.stack((a, b, c))
>>> np.max(stacked_matrices, axis=0)
array([4, 5, 8])
有关更多信息,请参见https://docs.scipy.org/doc/numpy/reference/generated/numpy.stack.html#numpy.stack。
(此答案假设数组是一维的。)