I have two data frames. Dataframe A is of shape (1269345,5) and dataframe B is of shape (18583586, 3).
Dataframe A looks like:
Name. gender start_coordinate end_coordinate ID
Peter M 30 150 1
Hugo M 4500 6000 2
Jennie F 300 700 3
Dataframe (B) looks like
ID_sim. position string
1 89 aa
4 568 bb
5 938437 cc
I want to make extract rows and make two data frames for which position column in dataframe B falls in the interval (specified by start_coordinate and end_coordinate column) in dataframe A.So resulting dataframe would look like:
###Final dataframe A
Name. gender start_coordinate end_coordinate ID
Peter M 30 150 1
Jennie F 300 700 3
###Final dataframe B
ID_sim. position string
1 89 aa
4 568 bb
I tried using numpy broadcasting like this:
s, e = dfA[['start_coordinate', 'end_coordinate']].to_numpy().T
p = dfB['position'].to_numpy()[:, None]
dfB[((p >= s) & (p <= e)).any(1)]
But this gave me the following error:
MemoryError: Unable to allocate 2.72 TiB for an array with shape (18583586, 160711) and data type bool
I think its because my numpy becomes quite large when I try broadcasting. How can I achieve my task without numpy broadcasting considering that my dataframes are very large. Insights will be appreciated.