Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas: change one column conditional on another

Tags:

How can I reverse the sign in the quantity column whenever the side column has a sell? All other values should not be changed. The following is simply not working, it has no effect.

df[df['side'] == 'Sell']['quantity'] = df[df['side'] == 'Sell']['quantity'] * -1
like image 913
Nickpick Avatar asked Sep 09 '16 15:09

Nickpick


1 Answers

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

df.loc[df.side == 'Sell', 'quantity'] *= -1
like image 65
Alexander Avatar answered Sep 23 '22 16:09

Alexander