passing multiple "on" conditions in merge_asof
Is there any way to pass multiple on conditions to merge_asof with either similar or different tolerances?
Let's consider df1 as:
| by1 | by2 | on1 | on2 |
|---|---|---|---|
| a | 1 | 1.1 | 1.2 |
| b | 2 | 1.3 | 1.4 |
| a | 3 | 1.5 | 1.6 |
and df2 as:
| by1 | by2 | on1 | on2 |
|---|---|---|---|
| a | 1 | 2.1 | 2.2 |
| x | 9 | 1.3 | 1.4 |
| a | 3 | 2.5 | 6.6 |
by a tolerance of 1 for on1 and 2 for on2, I want such merge:
| by1 | by2 | on1 | on2 |
|---|---|---|---|
| a | 1 | 2.1 | 2.2 |
I have tried other methods besides merge_asof, such as nested loops, which are definitely not time efficient. currently I'm using this code which only works for only one of my "on: conditions, in this case "on1":
df1= df1.sort_values(["on1"])
df2= df2.sort_values(["on1"])
df3 = pd.merge_asof(df1, CaDdDfChanges, on=["on1"], by=["by1", "by2"], tolerance=ToleranceFor1, direction="nearest")
from Recent Questions - Stack Overflow https://ift.tt/3DXJaZv
https://ift.tt/eA8V8J
Comments
Post a Comment