Without example data, it's unclear what you're trying. But using the operations in your for
loop, it could probably be done like this instead, without any loop:
myValue = df['myCol'] # the column you wanted and other calculations
df['myCol'] = df['myCol'].shift() - myValue
Depending on what you're trying, one of these should be what you want:
# starting with this df
myCol otherCol
0 2 6
1 9 3
2 4 8
3 2 8
4 1 7
# next row minus current row
df['myCol'] = df['myCol'].shift(-1) - df['myCol']
df
# result:
myCol otherCol
0 7.0 6
1 -5.0 3
2 -2.0 8
3 -1.0 8
4 NaN 7
or
# previous row minus current row
df['myCol'] = df['myCol'].shift() - df['myCol']
df
# result:
myCol otherCol
0 NaN 6
1 -7.0 3
2 5.0 8
3 2.0 8
4 1.0 7
And myVal
can be anything, like some mathematical operations vectorised over an entire column:
myVal = df['myCol'] * 2 + 3
# myVal is:
0 7
1 21
2 11
3 7
4 5
Name: myCol, dtype: int32
df['myCol'] = df['myCol'].shift(-1) - myVal
df
myCol otherCol
0 2.0 6
1 -17.0 3
2 -9.0 8
3 -6.0 8
4 NaN 7