Alessandrini
Alessandrini

Reputation: 191

Python - Using isin on my dataframe throws an error

I have a dataframe, and one of the columns is a list. I am trying to use isin but I keep getting "TypeError: unhashable type: 'list'"

  df = pd.DataFrame({'A': [[5,4,3,6], [7,8,9,0]], 'B': [[1,2,3,5], [1,2,6,8]]})

  df[df['A'].isin([3, 6])]

The intended output should be:

      A           B
1  [5,4,3,6]  [1,2,3,5]

How would I go about solving this problem, I have tried to use apply but I can't figure it out?

Upvotes: 0

Views: 356

Answers (1)

Chris Adams
Chris Adams

Reputation: 18647

IIUC, you could use set instead of list... for all keys to exist in the list try using set.issubset:

df[df['A'].apply(lambda x: {'3', '6'}.issubset(set(x)))]
# shorthand df[df['A'].apply(lambda x: {'3', '6'} < set(x))]

Example

df = pd.DataFrame({'A': [['5','6','3','4'], ['1','2','1','3']], 'B': [[1,2,3,5],[6,7,8,9]]})

#              A             B
# 0  [5, 6, 3, 4]  [1, 2, 3, 5]
# 1  [1, 2, 1, 3]  [6, 7, 8, 9]

df[df['A'].apply(lambda x: {'3', '6'}.issubset(set(x)))]

[out]

              A             B
0  [5, 6, 3, 4]  [1, 2, 3, 5]

If you only require at least one of the keys to exist, use set.intersection:

df[df['A'].apply(lambda x: bool({'3', '6'}.intersection(set(x))))]
# shorthand df[df['A'].apply(lambda x: bool({'3', '6'} & set(x)))]

[out]

              A             B
0  [5, 6, 3, 4]  [1, 2, 3, 5]
1  [1, 2, 1, 3]  [6, 7, 8, 9]

Upvotes: 3

Related Questions