Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculations inside loop with pandas

Tags:

python

pandas

I've this data set:

data = {
    'index': [4, 17, 24, 36, 42],
    'High': [805.000000, 1094.939941, 1243.489990, 1201.949951, 1172.839966],
}

And I would like to get a slope, like:

test = pd.DataFrame(data)

for i in range(len(test)):
    test.loc[:,'slope'] = (test.loc[i+1,'High'] - test.loc[i,'High'])   / (test.loc[i+1,'index'] - test.loc[i,'index'])

print(test)

Seems that I'm going out of the boundaries of the loop, but how can I code this in order to get the first row blank and fill the next?

If I do the same code without the +1 and use i instead it works, gives a 0/0 (Nan), but works.

The expected output should be:

expected output

like image 778
Artrade Avatar asked Nov 29 '25 20:11

Artrade


1 Answers

A whole-column way to compute this is like this:

We can use diff to make a series of differences vs the previous value:

test['index'].diff()
0     NaN
1    13.0
2     7.0
3    12.0
4     6.0
Name: index, dtype: float64

Using that we can compute the High difference over the index difference per step:

test['High'].diff() / test['index'].diff()
0          NaN
1    22.303072
2    21.221436
3    -3.461670
4    -4.851664
dtype: float64

It's an arbitrary choice IMO about where the index alignment should be - should this sequence start at index 0 or 1? But what you expect in the question is that it starts with 1, like in the result here.

like image 122
ramslök Avatar answered Dec 02 '25 10:12

ramslök



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!