Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Pandas start calculation from second row

I want to skip the first row (row 0), that contain the ‘100’ value in ‘index_100’, and start calculating from the row 1. My calculation uses the first row.

If I can start from the second row, this would be my function:

df['index_100'] = ((df['index_100']).shift(1))*(df['close'])/(df['prev_close'])

My df is like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      symbol      timestamp     close  index_100  prev_close
0    BTCUSDT  1665187200000  19537.11        100         NaN
1    BTCUSDT  1665187500000  19559.57        100    19537.11
2    BTCUSDT  1665187800000  19561.30        100    19559.57
3    BTCUSDT  1665188100000  19568.50        100    19561.30
4    BTCUSDT  1665188400000  19608.70        100    19568.50
5    BTCUSDT  1665188700000  19593.45        100    19608.70

Expected result:

      symbol      timestamp     close           index_100      prev_close
0    BTCUSDT  1665187200000  19537.11                 100             NaN
1    BTCUSDT  1665187500000  19559.57      100.1149607081        19537.11
2    BTCUSDT  1665187800000  19561.30   100.1238156513100        19559.57
3    BTCUSDT  1665188100000  19568.50      100.1606685943        19561.30
4    BTCUSDT  1665188400000  19608.70       100.366430859        19568.50
5    BTCUSDT  1665188700000  19593.45      100.2883742785        19608.70

>Solution :

Something like:

df['index_100'] = df['index_100'].mul(df['close']).div(df['close'].shift().bfill())

output:

    symbol      timestamp     close   index_100  prev_close
0  BTCUSDT  1665187200000  19537.11  100.000000         NaN
1  BTCUSDT  1665187500000  19559.57  100.114961    19537.11
2  BTCUSDT  1665187800000  19561.30  100.008845    19559.57
3  BTCUSDT  1665188100000  19568.50  100.036807    19561.30
4  BTCUSDT  1665188400000  19608.70  100.205432    19568.50
5  BTCUSDT  1665188700000  19593.45   99.922228    19608.70
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading