This post is a continuation on this question here.
I have a data frame having three columns named 'Altitude', 'Distance', 'Slope'. The column of 'Slope' should be calculated using the first two columns 'Altitude', 'Distance'.
Firstly, the purpose was to calculate 'Slope' using a condition explained as: A condition function was deployed to start from the top column of the "Distance" variable and add up (sum) values until the summation of them is greater or equal to 10 (>=10). If this condition is True, then calculate the "Slope" using the given formula: Slope=(Differential of 'Altitude')/(sum(Distance))
.
The summation of the 'Distance' starts from first value, till the index where the sum of 'Distance' is >= 10. Also, the Differential 'Altitude' means: Last value of Altitude - First value of Altitude
in each division.
I tried to draw a figure to illustrate better.
[![enter image description here][1]][1]
The following code (By Tim Roberts) is what was done originally. It uses 'KM_mean' formula. I am looking to correct and revise it in this post using my explanation above:
data = [
[11.2, 0],
[11.2, 3.018],
[10.9, 4.18],
[10.1, 4.873],
[9.9 , 5.499],
[9.4 , 5.923],
[9.2 , 6.415],
[8.5 , 1.063],
[8.4 , 1.667],
[7.9 , 3.114]
]
df = pd.DataFrame( data, columns=['Altitude','Distance'])
print( df )
s=[]
sumdist = 0
sumalt = 0
cntx = 0
for i in list(range(df.shape[0])):
sumdist += df.loc[i,'Distance']
sumalt += df.loc[i,'Altitude']
cntx += 1
if sumdist >= 10:
KM_mean = sumalt / cntx / sumdist
s.append(KM_mean)
sumdist = sumalt = 0
cntx = 0
if cntx:
s.append( sumalt / cntx / sumdist )
print(s)
[1]: https://i.stack.imgur.com/OPdXw.jpg