Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas select rows where query is in column of tuples

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: []
like image 817
archienorman Avatar asked Mar 08 '17 14:03

archienorman


1 Answers

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)
like image 196
jezrael Avatar answered Oct 27 '22 00:10

jezrael