I'm trying to create a column in a dataframe that is a result of dividing values based on another value in the dataframe.
So this means that i would like to divide the SCI value that has a corrosponding Temp value between 19.5 and 20.5 and equal chainage.
I created a small dataframe that could help solve the problem.
data = {'Chainage':[10,20,30,10,20,30,10,20,30], 'SCI':[123, 45, 19, 18, 36, 125, 54, 78,85], 'Temp':[20.4,35,16,22,20.1,19.8,18,21,28]}
df = pd.DataFrame(data)
The dataframe:
Chainage SCI Temp
0 10 123 20.4
1 20 45 35.0
2 30 19 16.0
3 10 18 22.0
4 20 36 20.1
5 30 125 19.8
6 10 54 18.0
7 20 78 21.0
8 30 85 28.0
Here is the end result as it should be. Grouped by the chainage, and then the SCI values with a Temp between 19.5 and 20.5 is used to divide with the others in the group. I have tried to illustrate below:
Chainage SCI Temp f
0 10 123 20.4 123/123 = 1
3 10 18 22.0 123/18 = 6.8
6 10 54 18.0 123/54 = 2.2
7 20 78 21.0 36/78 = 0.4
1 20 45 35.0 36/45 = 0.8
4 20 36 20.1 36/36 = 1
2 30 19 16.0 6.6
5 30 125 19.8 1
8 30 85 28.0 1.5
I have been trying to use the groupby but gets stuck when adding the extra conditioning. Any help is appreciated.