gpt4 book ai didi

python - Pandas 在 Column 的特定行范围内搜索最大值

转载 作者:行者123 更新时间:2023-12-02 17:13:26 25 4
gpt4 key购买 nike

我有 5 列 x 行的系列数据

import pandas as pd
data = {'name' : ['bill', 'joe', 'steve', 'ana', 'john', 'ray'],
'test1' : [85, 75, 85, 87,75, 81],
'test2' : [35, 45, 83, 35, 45, 83],
'test3' : [51, 61, 45, 51, 91, 45],
'Highscore' : [85 , 75, 85, 87, 91, 83],
}
frame = pd.DataFrame(data)
print(frame)

这是打印结果

        name  test1  test2  test3  HighScore
0 bill 85 35 51 85
1 joe 75 45 61 75
2 steve 85 83 45 85
3 ana 87 35 51 87
4 john 75 45 91 91
5 ray 81 83 45 83

如何仅在特定行范围内获取列中的最大值?

test1 from 0 to 3 --> 87
test2 from 3 to 5 --> 83
Highscore from 0 to 3 --> 87

最佳答案

您需要使用 loc 选择值并得到 max:

a = df.loc[0:3, 'test1'].max()
print (a)
87

b = df.loc[3:5, 'test2'].max()
print (b)
83
c = df.loc[0:3, 'HighScore'].max()
print (c)
87

关于python - Pandas 在 Column 的特定行范围内搜索最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47953727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com