Python python - How to check if a "tuple" of elements of two Series is contained in a set?

1

I have a "set" of "tuples" as follows set = {(2, 3), (2, 4), (5, 5)}

My DataFrame looks like this:

df = pd.DataFrame( {'one': [1,2,4,1], 'two' : [5,3,2,2]})

I want to check if in each line the "tuple" formed by the elements of the two series is contained in the set. The desired result would be this:

df = pd.DataFrame({'one': [1,2,4,1], 'two' : [5,3,2,2], 'three' : [False, True, False, False]})

I've tried:

df['three'] =  ((df['one'], df['two']) in set)

It did not work and gave me the following error message: 'Series' objects are mutable, so they can not be hashed "

How to achieve the desired result? Thanks

    
asked by anonymous 15.07.2018 / 01:57

2 answers

0

I found a solution:

df['A'] = list(zip(df.one, df.two))
df['three'] = df['A'].isin(set)
    
15.07.2018 / 02:44
0

Using zip and lists in understanding:

z=zip(......)
df['three'] = [x in set for x in z]
    
18.07.2018 / 19:59