Hi i got the data frame like this
import pandas as pd
data = [(1,"tom", 23),
(1,"nick", 12),
(1,"jim",13),
(2,"tom", 44),
(2,"nick", 56),
(2,"jim",77),
(3, "tom", 88),
(3, "nick", 10),
(3, "jim", 13),
]
df = pd.DataFrame(data, columns=['class', 'Name','number']
output of this dataframe
class Name number
0 1 tom 23
1 1 nick 12
2 1 jim 13
3 2 tom 44
4 2 nick 56
5 2 jim 77
6 3 tom 88
7 3 nick 10
8 3 jim 1
how can i get the maximum number of in 3 different name of class 1 and get the number but in same name but different class the results can be look like this
[name =tom, class=1, number =23]
[name =tom, class=2, number =44]
[name =tom, class=3, number =88]
Thank you very much for helping me!
CodePudding user response:
Find the name first from class 1, and then filter:
name = df.Name.loc[df[df['class'] == 1].number.idxmax()]
df[df.Name == name]
# class Name number
#0 1 tom 23
#3 2 tom 44
#6 3 tom 88
CodePudding user response:
Try tis.
idx = df.groupby(['class'])['number'].transform(max) == df['number']
df[idx]