2

I am trying to search for the row of which contains a string within a list.

df:

           Column1            Out1
0          ['x', 'y']         (0, 2)
1          ['a', 'b']         (3, 0)
etc.

I have attempted the following to search for the row that contains 'a' in a list under Column1, as suggested in this answer:

print df['Column1'].isin(['a'])

With the expectation of such an outcome:

1         ['a', 'b']          (3, 0)

However, I seem to be receiving the following error:

TypeError: unhashable type: 'list'
Enigmatic
  • 3,902
  • 6
  • 26
  • 48

2 Answers2

3

Need in for check values in lists:

df = df[df['Column1'].apply(lambda x: 'a' in x)]

Sample:

df = pd.DataFrame({'Column1':[['x','y'], ['a','b']],
                   'Out1':[(0,2), (3,0)]})
print (df)
  Column1    Out1
0  [x, y]  (0, 2)
1  [a, b]  (3, 0)

df1 = df[df['Column1'].apply(lambda x: 'a' in x)]
print (df1)
  Column1    Out1
1  [a, b]  (3, 0)

df1 = df[['a' in x for x in df['Column1']]]
print (df1)
  Column1    Out1
1  [a, b]  (3, 0)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

What you did in the beginning is correct:

df['Column1'].isin(['a'])

but you to wrap it with square brackets of the same dataframe.

here is an example:

threads = ['1111', '2222', '3333', '4444']
dev_data = train[(train['thread_col'].isin(threads))]

(dev_data) is a dataframe contains the results' rows.

Minions
  • 5,104
  • 5
  • 50
  • 91