Reputation: 127
Let's say this is my df
A B
0 a (33,32)
1 b (44,33)
2 a 32
3 a (66,33)
4 b (77,34,12)
5 c (88,1,55)
where column B values are tuples and I need to get something like df[df['B']contain 33]
A B
0 a (33,32)
1 b (44,33)
3 a (66,33)
is there any way to achieve this?
Upvotes: 3
Views: 925
Reputation: 862601
If there is mix numbers with tuples first convert integers to tuples:
df['B'] = df['B'].apply(lambda x: x if isinstance(x, tuple) else (x,))
And then filter values in tuples like:
df = df[df.B.apply(lambda x: 33 in x)]
Or:
df = df[[33 in x for x in df.B]]
Or:
df = df[pd.DataFrame(df.B.tolist()).eq(33).any(axis=1)]
print (df)
A B
0 a (33, 32)
1 b (44, 33)
3 a (66, 33)
Upvotes: 2