Viktor.w
Viktor.w

Reputation: 2297

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:

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

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

Upvotes: 1

Views: 101

Answers (1)

yatu
yatu

Reputation: 88276

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

Upvotes: 1

Related Questions