Reputation: 2253
I have a df
as follows:
ContextID EscRF_P2P_Volt_V StepID Time_Elapsed
7289972 12.45421 1 0
7289972 12.45421 1 0.055
7289972 12.45421 2 0.156
7289972 12.45421 2 0.487
7289972 12.45421 2 1.477
7289972 12.45421 2 2.477
7289972 13.18681 2 3.477
7289972 12.45421 2 4.487
7289972 12.45421 2 5.993
7289972 12.45421 2 6.545
7289972 12.45421 5 7.983
7289972 12.45421 5 8.993
7289972 13.18681 5 9.993
7289972 13.18681 5 10.393
7289972 12.45421 5 11.993
7289972 12.45421 5 13.093
7289972 12.45421 5 13.384
7289972 12.45421 5 14.388
7289972 12.45421 5 15.386
7289972 12.45421 5 16.386
7289972 12.45421 5 17.396
7289972 12.45421 5 18.406
7289972 12.45421 5 19.396
7289972 11.72161 5 20.396
7289972 12.45421 5 21.396
7289972 12.45421 7 22.386
7289972 12.45421 7 23.456
7289972 13.18681 7 24.404
7289972 12.45421 12 25.443
7289972 13.18681 12 26.443
7289972 11.72161 12 27.443
7289972 12.45421 12 28.453
7289972 13.18681 12 29.443
7289972 12.45421 12 30.443
7289972 12.45421 12 31.443
7289972 12.45421 15 32.472
7289972 27.10623 15 33.444
7289972 27.10623 16 34.443
7289972 22.71062 16 35.443
7289972 22.71062 17 36.443
7289972 622.7106 19 37.503
7289972 622.7106 19 38.513
7289972 622.7106 19 39.503
7289972 622.7106 19 40.503
7289972 622.7106 19 41.503
7289972 622.7106 19 42.503
7289972 622.7106 19 43.503
7289972 622.7106 19 44.503
7289972 622.7106 19 45.532
7289972 622.7106 19 46.502
7289972 622.7106 19 47.501
7289972 622.7106 19 48.501
7289972 622.7106 19 49.501
7289972 622.7106 19 50.501
What I would like to do is calculate the range of Time_Elapsed
and split it in 10 parts and calculate the slope of each part, with x
being the Time_Elapsed
& y
being EscRF_P2P_Volt_V
column.
I know I can define the slope as:
def slope(x1, y1, x2, y2):
m = (y2-y1)/(x2-x1)
return m
But I am not able to implement it correctly.
Any suggestions as to how can it be done?
Output:
The output for the first interval between 0-5 must be something like this:
slope = (12.45421-12.45421)/(5-0)
For the second interval between 5-10
slope = (13.18681-12.45421)/(10-5)
and so on...
If there is no exact value in Time_Elapsed
, like there is no 10
, so in that case we take the EscRF_P2P_Volt_V
value at 9.993
Upvotes: 0
Views: 84
Reputation: 10860
You could add another column with 10 Group IDs, then use groupby
and calculate the last minus first of the EscRF_P2P_Volt_V column in each Group divided by the last minus first of the Time_Elapsed column:
df['grpNo'] = df.Time_Elapsed // 5.0502
a more general approach for the grpNo calculation if you have a normal counting index from 0 ... n-1 (but you didn't post that):
df['grpNo'] = df.index.values // (len(df)/10)
But note that they do not result in the same grouping, so the results of the slopes vary, too. It's up to you to implement the grouping you'd like to use...
grpd = df.groupby('grpNo')
(grpd.EscRF_P2P_Volt_V.last() - grpd.EscRF_P2P_Volt_V.first()) / (grpd.Time_Elapsed.last() - grpd.Time_Elapsed.first())
# grpNo
# 0.0 0.000000
# 1.0 0.183150
# 2.0 -0.183379
# 3.0 0.000000
# 4.0 0.365569
# 5.0 0.183150
# 6.0 3.663005
# 7.0 147.783246
# 8.0 0.000000
# 9.0 0.000000
# dtype: float64
result with index based grouping
# grpNo
# 0.0 0.000000
# 1.0 -0.162583
# 2.0 0.000000
# 3.0 0.000000
# 4.0 0.146286
# 5.0 0.183150
# 6.0 3.663005
# 7.0 118.577071
# 8.0 0.000000
# 9.0 0.000000
# dtype: float64
comparison of the group sizes of the different groupings here:
Index based Time based
0 6 8
1 5 5
2 6 5
3 5 5
4 6 5
5 5 5
6 5 5
7 6 5
8 5 5
9 5 6
Upvotes: 1