Pandas在Column中的某个行范围内搜索最大值

问题描述 投票:0回答:1

我有来自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

如何在某些行范围内获取Column中的最大值?

test1 from 0 to 3 --> 87
test2 from 3 to 5 --> 83
Highscore from 0 to 3 --> 87
python python-3.x pandas
1个回答
4
投票

您需要使用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
© www.soinside.com 2019 - 2024. All rights reserved.