Skip to content

Instantly share code, notes, and snippets.

@x
Created April 30, 2021 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x/6075ef8f7dbb288e97e79149b7f72b7e to your computer and use it in GitHub Desktop.
Save x/6075ef8f7dbb288e97e79149b7f72b7e to your computer and use it in GitHub Desktop.
pandas range inner join
In [3]: df1 = pd.DataFrame({"start": [1,2,6], "end": [2,6,9]})
...: df2 = pd.DataFrame({"foo": [1,2,3,4,5,6,7,8,9]})
...: dfm = pd.merge(df1, df2, how="cross")
...: dfm.loc[(dfm.foo >= dfm.start) & (dfm.foo < dfm.end)]
Out[3]:
start end foo
0 1 2 1
10 2 6 2
11 2 6 3
12 2 6 4
13 2 6 5
23 6 9 6
24 6 9 7
25 6 9 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment