I have a dataframe with a column of tuples (df.row_col) that I need to search using a list of tuples. If a tuple from the list is present in the dataframe column, I want to return that row and add a new column to the dataframe. I tried this list comprehension, but I'm not sure if I can loop through a list like this. Much appreciate the help!
data_tuples=
[(7, 45),
(13, 34),
(17, 51),
(17, 52),
(17, 53),
(17, 54),
(17, 55),
(18, 50)]
Dataframe to search:
index farm layer row column Qmax row_col
0 1 1 3 7 36 0.0 (7, 36)
1 2 1 3 7 37 0.0 (7, 37)
2 3 1 3 8 35 0.0 (8, 35)
3 4 1 3 8 36 0.0 (8, 36)
4 5 1 3 8 37 0.0 (8, 37)
for tup in data_tuples:
new_df = df[df["row_col"].apply(lambda x: True if tup in x else False)]
return new_df