Python Pandas start calculation from second row

Question:

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:

      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
Asked By: One boy

||

Answers:

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
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.