I have two pandas series:
s1 with potentially a very large number of rows and some NaN and s2 (which is a column in a dataframe (df) with only 20 rows.
The index of the two series differ.
s1:
id
1 4.5
2 15.0
3 13.0
4 14.0
5 18.0
6 15.0
7 13.0
8 14.0
9 NaN
10 NaN
11 NaN
12 18.0
13 NaN
14 NaN
15 NaN
df:
col1 s2
0 20.0 0.0
1 19.0 4.5
2 18.0 5.0
3 17.0 6.0
4 16.0 7.0
5 15.0 8.0
6 14.0 9.0
7 13.0 10.0
8 12.0 11.0
9 11.0 12.0
10 10.0 13.0
11 9.0 15.0
12 8.0 16.0
13 7.0 18.0
14 6.0 20.0
15 5.0 22.0
16 4.0 24.0
17 3.0 26.0
18 2.0 28.0
19 1.0 100.0
For each id of s1 I want to retrieve the value of col1 for the first element in s2 smaller or equal to id.
i.e. for id 1 we have s1 = 4.5, which is smaller or equal than df.s2 = 4.5, hence I want to retrieve the value 19.
Accordingly, for id=2 in s1 I need to retrieve the value 9 in df.col1
This is my current solution. I am wondering if there is a better (faster, maybe a pandas function?) way to get the same result:
output = [min(df[df['s2'].le(element)].col1, default = np.NaN) for element in s1]
[19.0,
9.0,
10.0,
10.0,
7.0,
9.0,
10.0,
10.0,
nan,
nan,
nan,
7.0,
nan,
nan,
nan]