数据['col'].values中的字符串,但数据['col']中的字符串 [英] String in data['col'].values but not in data['col']
问题描述
我已经从文件中读取了一个熊猫数据框:
I've read a Pandas dataframe from file:
df = pd.read_csv('data_here.csv')
当我尝试"str2try" in df['col2search']
时,它返回False
,但是当我尝试"str2try" in df['col2search'].values
时,它返回True
(这是本例中的期望值).
When I try "str2try" in df['col2search']
it returns False
, but when I try "str2try" in df['col2search'].values
, it returns True
(and this is what I'd expect in this case).
我不明白为什么会有行为上的差异;我读到.values
返回该列的Numpy表示形式,但是为什么"str2try" in <NDFrame representation of column>
返回False
?
I don't see why there would be a behavioral difference; I read that .values
returns the Numpy representation of the column, but why does "str2try" in <NDFrame representation of column>
return False
?
谢谢!
推荐答案
Pandas系列就像一本字典. in
搜索其索引(或键),因此"str2try" in df['col2search']
检查字符串是否在该系列的 index 中:
A pandas Series is like a dictionary. in
searches its index (or keys) so "str2try" in df['col2search']
checks whether the string is in the index of that Series:
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
这是它在字典中的表现方式:
Here's how it would behave in a dictionary:
d = {'x': 1, 'y': 2, 'z': 3}
'x' in d
Out: True
2 in d
Out: False
2 in d.values()
Out: True
这篇关于数据['col'].values中的字符串,但数据['col']中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!