Check if values of two columns are in a list of couples of values : pandas dataframe

65 views Asked by At

I have a list of couples of values, and DataFrame.

  • I want to create a new DataFrame d2 containing the rows of existing DataFrame d1 where values of columns ['A','B'] must be equal (both A and B at the same time) to at least one of couples in a list c of values.

Every tuto I see check if values in a DataFrame match a fixed one-or-two-pair of values at max.

For example :

c = [[1, 100], [2,200], [3, 300], ..., [5,500]]
dfa = pd.DataFrame({"A": [1, 2, 3, 4, 5, 6], "B": [100, 200, 300, 400, 500, 600]})

At the moment, I did :

dfa[dfa[['A', 'B']].isin(c).all(axis=1)]

But it returns nothing, whereas it should return two rows ! I appreciate any help :)

1

There are 1 answers

0
Bushmaster On

You can use:

dfa["new_col"] = dfa.values.tolist() #Column (A,B) values into a list in a new column
out = dfa[dfa["new_col"].isin(c)]