I have a data frame including three columns named 'Altitude', 'Distance', 'Slope'. The column of 'Slope' is calculated using the two first columns 'Altitude', 'Distance'. @ the first step the purpose was to calculate 'Slope' using a condition explained below: 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 corrects then calculate the "Slope" using the given formula: Slope=Average(Altitude)/(sum(Distance)). The summation of the 'Distance' was counting from the first value of that to the index that the 'Distance' has stopped there). The following code is for the above explanation (By Tim Roberts):
Altitude Distance
0 11.2 0.000
1 11.2 3.018
2 10.9 4.180
3 10.1 4.873
4 9.9 5.499
5 9.4 5.923
6 9.2 6.415
7 8.5 1.063
8 8.4 1.667
9 7.9 3.114
import pandas as pd
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)
Output: SLOPE: [0.8988484798276862, 0.8448607949571003, 0.6933681376947548]
My QUESTION: Then The next PART:
I am going to repeat the received number from the code: [0.8988484798276862, 0.8448607949571003, 0.6933681376947548]
. I am looking to repeat each figure by the number of rows associated with it. for example, 0.8988484798276862
will be repeated four times in a new column then 0.8448607949571003
will be repeated two times and so on
I have written a code below but it returns me empty values:
RR=[]
for i in list(range(df.shape[0])):
sumdist += df.loc[i,'Distance']
sumalt += df.loc[i,'Altitude']
cntx += 1
if sumdist >= 10:
R_s=np.repeat(df['Slope'].to_numpy())
RR.append(R_s)
RR=DataFrame(RR)