Home > Back-end >  Python Pandas start calculation from second row
Python Pandas start calculation from second row

Time:10-09

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

CodePudding user response:

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
  • Related