1

I'm having trouble to make my basic trading bot. I've managed to create the column buy/sell for the rows to buy or sell (https://i.stack.imgur.com/e7vg4.jpg) to select the rows I use:

  df.loc[df['Buy/Sell'] == 'Buy']

But now for these rows I want to add the df['Open'] value to the df['portfolio']

I tried the following:

 df.loc[(df['Buy/Sell'] == 'Sell'),'Portfolio'] = df['Portfolio'] + df['Open']

It turned out it didn't select anything and just did this to all rows I hope someone can help me, thanks in advance

Cimbali
  • 11,012
  • 1
  • 39
  • 68
  • Please [do not post images](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) of your data or errors. You can include [code that creates a dataframe such as `df.to_dict()` or the output of `print(df)`](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) (include at least the few rows and columns that allow to reproduce your issue) – Cimbali Oct 02 '21 at 09:36

1 Answers1

1

This might be what you require:

df.loc[df['Buy/Sell'] == 'Sell', 'Portfolio'] = df['Portfolio'].add(df['Open'])

Please consider rewriting your code to the above.

sophocles
  • 13,593
  • 3
  • 14
  • 33