I've seen a bunch of questions with similar titles but I still cant figure it out. All I want to do is replace the value thats in my dataframe in the fifth row and 5th column with the value 100. I thought simply this would do the trick
df.loc['cheerios','rating']= 100
since cheerios is the row and rating is the column
name sugar sodium rating
0 fruit loop x x x
1 trix x x x
2 oreo x x x
3 cocoa puff x x x
4 cheerio x x 100
Pandas DataFrame replace() Method The replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.
(3) Replace multiple values with multiple new values for an individual DataFrame column: df['column name'] = df['column name']. replace(['1st old value','2nd old value',...],['1st new value','2nd new value',...])
.loc
is an indexer. It looks for an entry in the index, but the column name
is not an index. It is simply a column. The following solutions would work:
df.loc[4, 'rating'] = 100 # Because 4 is in the index, but how do you know?
or:
df.loc[df['name']=='cheerio', 'rating'] = 100 # Find the row by column
or:
df.set_index('name', inplace=True) # Make 'name' the index
df.loc['cheerios', 'rating'] = 100 # Use the indexer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With