0

I want the result of the calculation profit starts from the second row so I can get the result of the last row in my data frame because in this current code the last value of the profit is incorrect, it must be calculating the first row + the second row of the column 'value' but I need the result to start in the second row of the column 'profit'.

xf['profit']= xf.value + xf.value.shift(-1).fillna(0)

[enter image description here](https://i.stack.imgur.com/OoGyx.png)

I have tried to make the result of calculating the first and second row in 'value' to be in the second row in 'profit' but that ruined my code.

gtomer
  • 5,643
  • 1
  • 10
  • 21
  • Please provide a minimal reproducible example of the input + the matching expected output – mozway Aug 14 '23 at 16:07
  • Refrain from showing your dataframe as an image. Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Aug 14 '23 at 19:28

1 Answers1

0

you can use this example

before = df.iloc[:-1,:]["value"].reset_index(drop=True)
after = df.iloc[1:,:]["value"].reset_index(drop=True)
df["profits"] = (before+after).shift(1).fillna(0)
df

or this

before = df.value.shift(1)
after = df.value
df["profits"] = (before+after).fillna(0)
df
SimoN SavioR
  • 614
  • 4
  • 6