bcf
bcf

Reputation: 2134

Select DataFrame column elements that are in a list

I am trying to select all elements of a pandas DataFrame df that are in a list l. I have tried the techniques below but they don't quite get me what I want:

import pandas as pd
df = pd.DataFrame( data = [ 'a', 'b', 'c', 'b', 'c', 'a' ], columns = [ 'char' ] )
l = [ 'a', 'b' ]

df.char == 'a'  # ok
df.char == 'b'  # ok
df.char == l  # not ok
df.char in l  # not ok

Running this:

>>> df
  char
0    a
1    b
2    c
3    b
4    c
5    a
>>> df.char == 'a'

0     True
1    False
2    False
3    False
4    False
5     True
Name: char, dtype: bool

>>> df.char == 'b'
0    False
1     True
2    False
3     True
4    False
5    False
Name: char, dtype: bool

>>> df.char == l
Traceback (most recent call last):
...
ValueError: Arrays were different lengths: 6 vs 2

>>> df.char in l
Traceback (most recent call last):
...
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

The desired output is:

>>> <correct code here>

0     True
1     True
2    False
3     True
4    False
5     True
Name: char, dtype: bool

Upvotes: 1

Views: 71

Answers (1)

Ian Thompson
Ian Thompson

Reputation: 3285

Try using .isin():

df.char.isin(l)

returns:

0     True
1     True
2    False
3     True
4    False
5     True

Upvotes: 3

Related Questions