Try to replace a specific value in a dataframe, but does not overwritte it

Viktor.w :

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:

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

But when I print the dataframe it is not changed... any idea why? thanks!

yatu :

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=376154&siteId=1