Reputation: 2507
I would like to change the values of a given column for the first 4 rows that appear for a specific boxer in my dataset
This is a sample of my data
name clean_outcome
Tyson Fury an
Other Boxer win
Tyson Fury an
Other Boxer win
Tyson Fury
Tyson Fury
Tyson Fury
Other Boxer
Tyson Fury
...
This is what I have tried:
fully_merged[fully_merged.name == 'Tyson Fury'].iloc[0:4]['clean_outcome'] = 'Win Other'
The ideal output would be this:
name clean_outcome
Tyson Fury Win Other
Tyson Fury Win Other
Tyson Fury Win Other
Tyson Fury Win Other
Upvotes: 2
Views: 43
Reputation: 153460
IIUC, use .loc with boolean indexing and column filtering,
df.loc[((df['name'] == 'Tyson Fury').cumsum() <= 4) &
(df['name'] == 'Tyson Fury'),
'clean_outcome'] = 'Win Other'
Output:
name clean_outcome
0 Tyson Fury Win Other
1 Other Boxer win
2 Tyson Fury Win Other
3 Other Boxer win
4 Tyson Fury Win Other
5 Tyson Fury Win Other
6 Tyson Fury None
7 Other Boxer None
8 Tyson Fury None
Upvotes: 1