dataframe groupby 后求每个key最大值所在的行号

dataframe groupby 后求每个key最大值所在的行号,用idxmax()函数

import pandas as pd
array = []
for charc in ['a', 'b', 'c']:
    for i in range(5):
        array.append([charc, i])
data = pd.DataFrame(array, columns=["name", 'score'])
print(data)
idx = data.groupby('name')['score'].idxmax()
data.loc[idx,'score'] = -1
print(data)
   name  score
0     a      0
1     a      1
2     a      2
3     a      3
4     a      4
5     b      0
6     b      1
7     b      2
8     b      3
9     b      4
10    c      0
11    c      1
12    c      2
13    c      3
14    c      4
   name  score
0     a      0
1     a      1
2     a      2
3     a      3
4     a     -1
5     b      0
6     b      1
7     b      2
8     b      3
9     b     -1
10    c      0
11    c      1
12    c      2
13    c      3
14    c     -1

你可能感兴趣的:(Pandas)