Python pandas: change one column to another

How can I change the sign in the quantity column whenever a side column sells? All other values ​​should not be changed. The following simply does not work, it does not work.

df[df['side'] == 'Sell']['quantity'] = df[df['side'] == 'Sell']['quantity'] * -1
+4
source share
1 answer

Refer to the Pandas indexing documentation and never use the chain indexing in your example when setting values.

df.loc[df.side == 'Sell', 'quantity'] *= -1
+3
source

Source: https://habr.com/ru/post/1654151/


All Articles