Reputation: 62596
I have a pandas dataframe "df" that contains:
SPORT, NAME, TEST
Tennis, A, TESTA
Tennis, B, TESTB
Basketball, C, TESTC
Volleyball, D, TESTC
I want to change the 'TEST' column only for the ones where SPORT == 'Tennis' to 'CHANGED'. I tried the following:
tennislist = df[df['SPORT'] == 'Tennis']
tennislist['TEST'] = 'CHANGED'
While tennislist appears to be updated with the correct values, when I then print out the original "df", I see no changes at all. How can I make it so that even though I filtered on tennislist to update the column 'TEST' that those changes also updates the original "df" that I filtered from?
Upvotes: 1
Views: 1024
Reputation: 5183
You can modify the values in df with
df.loc[df['SPORT'].eq('Tennis'), 'TEST'] = 'CHANGED'
Your code is currently creating a new object called tennislist
which is a subset of the rows in df
, but if you change tennislist
it will not be reflected in df
.
Upvotes: 1