I have a dataframe in which one column contains tuples:
df = pd.DataFrame({'a':[1,2, 3], 'b':[(1,2), (3,4), (0,4)]})
a b
0 1 (1, 2)
1 2 (3, 4)
2 3 (0, 4)
I would like to select the rows where an element I provide is in the tuple.
For example, return rows where 4 is in a tuple, expect outcome would be:
a b
1 2 (3, 4)
2 3 (0, 4)
I have tried:
print(df[df['b'].isin([4])]
But this returns an empty dataframe:
Empty DataFrame
Columns: [a, b]
Index: []
You need apply
with in
:
print(df[df['b'].apply(lambda x: 4 in x)])
a b
1 2 (3, 4)
2 3 (0, 4)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With