It's quite hard to know what's going on in your example when you don't provide a sample dataframe. Below is how I would do it:
import random, pandas as pd
random.seed(0)
# for generating names
first_names = ['Adam', 'Bob', 'Charlie', 'David', 'Evan', 'Frank', 'George', 'Harry', 'Ivan', 'John', 'Kevin', 'Larry', 'Michael', 'Nathan', 'Oscar', 'Peter', 'Quentin', 'Robert', 'Steven', 'Thomas', 'Ulysses', 'Victor', 'William', 'Xavier', 'Yuri', 'Zachary']
last_names = ['Adams', 'Baker', 'Carter', 'Davis', 'Edwards', 'Franklin', 'Garcia', 'Harris', 'Ivanov', 'Johnson', 'Kowalski', 'Lee', 'Miller', 'Nelson', 'Olsen', 'Parker', 'Quinn', 'Robinson', 'Smith', 'Taylor', 'Ulyanov', 'Volkov', 'Williams', 'Xavier', 'Young', 'Zimmerman']
# generate sample data
df = pd.DataFrame({
'first_name': [random.choice(first_names) for _ in range(100)],
'last_name': [random.choice(last_names) for _ in range(100)],
'age': [random.randint(18, 35) for _ in range(100)],
'salary': [random.randint(1000, 10000) for _ in range(100)]
})
# get the rows where the salary is the max salary for each age
df.loc[df.groupby('age')['salary'].idxmax()]
Output:
first_name last_name age salary
84 Robert Smith 18 8849
13 George Parker 19 8792
61 Peter Lee 20 9986
4 Ivan Harris 21 9656
42 George Williams 22 7153
79 Kevin Adams 23 8883
63 Harry Franklin 24 5807
52 Charlie Kowalski 25 6541
50 Robert Smith 26 7016
38 Nathan Olsen 27 7102
87 David Carter 28 7988
39 Kevin Parker 29 9813
18 Yuri Edwards 30 8421
70 Harry Williams 31 9569
80 Quentin Davis 32 8766
22 Ivan Williams 33 9533
37 Larry Ivanov 34 8134
93 Thomas Garcia 35 7153