2017-06-20 45 views
0

我從文件中讀取一個數據幀的熊貓:字符串數據[「關口」]值,而不是在數據[「關口」]

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

謝謝!

回答

1

熊貓系列就像字典。 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 
+0

謝謝!這很有道理! –

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