Python data string rows

If I have a data frame with lines n, is there a way to install the i-th row as the sum row[i]and row[i-1]and make it so that the assignment of the earlier lines was reflected in the last lines? I would really like to avoid loops, if possible.

DF example:

             SPY   AAPL   GOOG
2011-01-10  0.44  -0.81   1.80
2011-01-11  0.00   0.00   0.00
2011-01-12 -1.11  -2.77  -0.86
2011-01-13 -0.91  -4.02  -0.68

Example of pseudocode for summing two lines:

DF[2011-01-11] = DF[2011-01-10] + DF[2011-01-11]
DF[2011-01-12] = DF[2011-01-11] + DF[2011-01-12]

etc.

+4
source share
2 answers

depending on your question, you are looking for the total amount of each column. you can use the methodcumsum()

DF.cumsum()

               SPY    AAPL   GOOG
2011-01-10  0.4400 -0.8100 1.8000
2011-01-11  0.4400 -0.8100 1.8000
2011-01-12 -0.6700 -3.5800 0.9400
2011-01-13 -1.5800 -7.6000 0.2600
+2
source

Use DF.shift()

DF + DF.shift()
+1
source

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


All Articles