I have two pandas DataFrames:
import pandas as pd
data1 = {
'score': [1, 2],
'seconds': [1140, 2100],
}
data2 = {
'prize': [5.5, 14.5, 14.6, 21, 23, 24, 26, 38, 39, 40, 50],
'seconds': [840, 1080, 1380, 1620, 1650, 1680, 1700, 1740, 2040, 2100, 2160],
}
df1 = pd.DataFrame.from_dict(data1)
df2 = pd.DataFrame.from_dict(data2)
Output: df1
score seconds
0 1 1140
1 2 2100
Output: df2
prize seconds
0 5.5 840
1 14.5 1080
2 14.6 1380
3 21.0 1620
4 23.0 1650
5 24.0 1680
6 26.0 1700
7 38.0 1740
8 39.0 2040
9 40.0 2100
10 50.0 2160
For each value in seconds
column from df1, I would like to get the match (or the closest to) row from df2 and also the closest 2 rows above and below the match.
The seconds columns contains only sorted unique values.
As result, I expect this:
Output: result
prize seconds
0 5.5 840
1 14.5 1080 # closest match to 1140
2 14.6 1380
3 21.0 1620
7 38.0 1740
8 39.0 2040
9 40.0 2100 # match 2100
10 50.0 2160