The situation is as follows.
I have two pandas dataframes:
- df1, which contains a column "p1" with 1895 rows of random numbers ranging from 2.805 to 3.035 (here are the first 20 rows):
p1
0 2.910
1 2.885
2 2.875
3 2.855
4 2.910
5 2.870
6 2.850
7 2.875
8 2.865
9 2.875
10 2.890
11 2.910
12 2.965
13 2.955
14 2.935
15 2.905
16 2.900
17 2.905
18 2.970
19 2.940
- df2, which contains two columns, "p2" and "h"
p2 h
0 2.7 256.88
1 2.8 253.52
2 2.9 250.18
3 3.0 246.86
4 3.1 243.55
The aim is to first loop through all rows in df1 and find the closest element in p2 for each row. e.g. for p1[0] = 2.910, the closest element is p2[2] = 2.9.
- Then, if these two values are the same, the output for that row is the corresponding value of h
- otherwise, the output is the average of the previous and subsequent values of h.
Going back to our example, the output for p1[0] should therefore be (h[1]+h[3])/2
I hope this all makes sense, this is my first question on here :). Thanks!