-1

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
Ank
  • 1,704
  • 9
  • 14
Allin
  • 53
  • 5
  • Hi @Allin Please edit your post to make it more understandable. The links to previous question and images are also not appearing. – Ank May 19 '21 at 17:29

1 Answers1

1

Going through the changes in calculation for 'Slope' that you mentioned in your previous question, I have modified the code accordingly. Try this one out:

sum_distance = 0
idx = 0
prev_idx = -1
slopes = []

for idx, i in enumerate(df.Distance.values):
    sum_distance += i
    if sum_distance >= 10:
        slopes += [(df.Altitude.iloc[idx] - df.Altitude.iloc[prev_idx + 1]) / sum_distance] * (idx - prev_idx)
        sum_distance = 0
        prev_idx = idx

if idx != prev_idx:
    slopes += [(df.Altitude.iloc[idx] - df.Altitude.iloc[prev_idx + 1]) / sum_distance] * (idx - prev_idx)
    
df['Slope'] = slopes

Output:

>>> df
   Altitude  Distance     Slope
0      11.2     0.000 -0.091127
1      11.2     3.018 -0.091127
2      10.9     4.180 -0.091127
3      10.1     4.873 -0.091127
4       9.9     5.499 -0.043775
5       9.4     5.923 -0.043775
6       9.2     6.415 -0.106045
7       8.5     1.063 -0.106045
8       8.4     1.667 -0.106045
9       7.9     3.114 -0.106045

The code sums 'Distance', and tracks whenever the sum is 10 or more, then calculates Differential 'Altitude' and finally Slope using the formula mentioned.

Ank
  • 1,704
  • 9
  • 14
  • 1
    Thanks Ank, it works perfectly. I just modified the question following link https://stackoverflow.com/questions/ask – Allin May 19 '21 at 22:23