选择列表中的DataFrame列元素

时间:2018-04-05 19:49:12

标签: python pandas

我正在尝试选择列表df中的pandas DataFrame l的所有元素。我尝试了下面的技术,但他们并没有让我得到我想要的东西:

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

运行此:

>>> 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().

所需的输出是:

>>> <correct code here>

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

1 个答案:

答案 0 :(得分:3)

尝试使用.isin()

df.char.isin(l)

返回:

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