Reputation: 723
I have two DataFrames like this:
df_cells = pd.DataFrame({
'left': [1095, 257],
'top': [1247, 1148],
'right': [1158, 616],
'bottom': [1273, 1176]
})
df_text = pd.DataFrame({
'words': ['Hello', 'world', 'nice day', 'have a'],
'left': [1097, 1099, 258, 259],
'top': [1248, 1249, 1156, 1153],
'right': [1154, 1156, 615, 614],
'bottom': [1269, 1271, 1175, 1172]
})
df_cells
contains coordinates of bounding boxes for phrases on an image and df_text
contains words and their bounding box coordinates on an image.
I have created a list of tuples where the bounding boxes for phrases and words match based on left
, top
, right
, bottom
values like this:
overlap = [(0,0), (1,0), (2, 1), (3, 1)]
where the first element of a tuple is the index value of df_text
and the second element is matching index value of df_cells
.
I want to select,combine rows based on overlap into a new dataframe like this:
Words df_cells.left df_cells.top df_cells.right df_cells.bottom
Hello 1095 1247 1158 1273
world 1095 1247 1158 1273
Have a 257 1148 616 1176
nice day 257 1148 616 1176
and afterwards concatenate words with the same left
, top
, right
, bottom
like this:
Words df_cells.left df_cells.top df_cells.right df_cells.bottom
Hello world 1095 1247 1158 1273
Have a nice day 257 1148 616 1176
Would appreciate some help with this.
Upvotes: 1
Views: 157
Reputation: 22503
I think you can assign the values directly to df_text
with index created by list comprehension:
df_text.iloc[[i[0] for i in overlap], 1:] = df_cells.iloc[[i[1] for i in overlap]].to_numpy()
print (df_text)
words left top right bottom
0 Hello 1095 1247 1158 1273
1 world 1095 1247 1158 1273
2 nice day 257 1148 616 1176
3 have a 257 1148 616 1176
print (df_text.groupby(["left", "top", "right", "bottom"], as_index=False).agg({"words":" ".join}))
left top right bottom words
0 257 1148 616 1176 nice day have a
1 1095 1247 1158 1273 Hello world
Upvotes: 1