Skip to content Skip to sidebar Skip to footer

Try To Replace A Specific Value In A Dataframe, But Does Not Overwritte It

My dataframe looks like this orders_total: price amount side 0 0.003019 100 bids 0 0.003143 100 asks When I try to replace a specific value by doing the following

Solution 1:

You're attempting to modify in-place when you really are ending up with a copy of the dataframe, not a view, and hence the original dataframe remains unchanged. This is known as chained indexing.

To find out more on this check: Returning a view versus a copy.

You want to index along both axis using .loc. This will lead to a single call to __getitem__ which will return a view of the dataframe, and changes to this view will be reflected on the original dataframe:

orders_total.loc[orders_total.side == 'asks', 'amount'] -= 10

Post a Comment for "Try To Replace A Specific Value In A Dataframe, But Does Not Overwritte It"