数据中的字符串[' col']。值但不包含数据[' col']

时间:2017-06-20 16:15:22

标签: python python-2.7 pandas numpy

我从文件中读取了Pandas数据框:

df = pd.read_csv('data_here.csv')

当我尝试"str2try" in df['col2search']时,它会返回False,但当我尝试"str2try" in df['col2search'].values时,它会返回True(这就是我在这种情况下所期望的)。

我不明白为什么会出现行为差异;我读到.values会返回列的Numpy表示,但为什么"str2try" in <NDFrame representation of column>会返回False

谢谢!

2 个答案:

答案 0 :(得分:2)

熊猫系列就像一本字典。 in搜索其索引(或键),以便"str2try" in df['col2search']检查该字符串是否在该系列的索引中:

df = pd.DataFrame({'A': [1, 2, 3]}, index=['x', 'y', 'z'])

df
Out: 
   A
x  1
y  2
z  3

'x' in df['A']
Out: True

2 in df['A']
Out: False

'x' in df['A'].values
Out: False

2 in df['A'].values
Out: True

以下是字典中的表现方式:

d = {'x': 1, 'y': 2, 'z': 3}

'x' in d
Out: True

2 in d
Out: False

2 in d.values()
Out: True

答案 1 :(得分:0)

迭代在列表或数组的情况下有效。请查看以下解释

import pandas as pd
frame = pd.DataFrame({'a' : ['the cat is blue', 'the sky is green', 'the dog is black']})
In [4]: f["a"]
Out[4]: 
0     the cat is blue
1    the sky is green
2    the dog is black
Name: a, dtype: object
In [5]: f["a"].values
Out[5]: array(['the cat is blue', 'the sky is green', 'the dog is black'], dtype=ob
ject)
In [6]: type(f["a"])
Out[6]: pandas.core.series.Series
In [7]: type(f["a"].values)
Out[7]: numpy.ndarray