我正在尝试在 python 中隔离并打印 pandas 数据框中的最大值。
# Data frame:
df
>> 0 A B C
0 0 0 0 0
A 0 -3 -3 5
B 0 -3 -6 2
C 0 5 0 -3
D 0 5 2 -3
E 0 0 10 5
F 0 -3 5 15
我已经设法用以下代码隔离值:
x = df.max(axis=0)
maxValue = max(x)
maxValue
>> 15
但是我怎样才能访问这个元素呢?有没有一种方法可以遍历数据框的元素,这样
for elements in df:
if element == maxValue:
m = element
或者那些线路上的东西?我需要找到最大的元素,在本例中为 15,并检索其位置,即本例中的 (C,F)。然后我需要存储它,然后找到第一个元素周围的下一个最大元素及其位置。
# desired output
[(C,F), (B,E), (A,D)]
我希望这是有道理的!任何关于我如何实现这一点的建议将不胜感激! :)
最佳答案
您可以使用:
#replace 'df.iloc[:,1:]' with 'df' if first column isnt 0
out = [*df.iloc[:,1:][::-1].idxmax().items()]
#[('A', 'D'), ('B', 'E'), ('C', 'F')]
关于python - 如何在 pandas python 库中隔离数据框中的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60547693/