繁体   English   中英

选择列表中的DataFrame列元素

[英]Select DataFrame column elements that are in a list

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

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

尝试使用.isin()

df.char.isin(l)

收益:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM