在 pandas 系列中查找值 - Python3
finding values in pandas series - Python3
我遇到了这个非常烦人的问题(我是 python 的新手)
df=pd.DataFrame[{'col1':['1','2','3','4']}]
col1=df['col1']
为什么 col1[1] in col1
return False
?
检查值使用 boolean indexing
:
#get value where index is 1
print (col1[1])
2
#more common with loc
print (col1.loc[1])
2
print (col1 == '2')
0 False
1 True
2 False
3 False
Name: col1, dtype: bool
如果需要获取行:
print (col1[col1 == '2'])
1 2
Name: col1, dtype: object
用or
检查多个值:
print (col1.isin(['2', '4']))
0 False
1 True
2 False
3 True
Name: col1, dtype: bool
print (col1[col1.isin(['2', '4'])])
1 2
3 4
Name: col1, dtype: object
还有一些关于 in
的测试会员资格 docs:
Using the Python in operator on a Series
tests for membership in the index, not membership among the values.
If this behavior is surprising, keep in mind that using in on a Python dictionary tests keys, not values, and Series are dict-like. To test for membership in the values, use the method isin():
For DataFrames, likewise, in applies to the column axis, testing for membership in the list of column names.
#1 is in index
print (1 in col1)
True
#5 is not in index
print (5 in col1)
False
#string 2 is not in index
print ('2' in col1)
False
#number 2 is in index
print (2 in col1)
True
您尝试在索引值中查找字符串 2
:
print (col1[1])
2
print (type(col1[1]))
<class 'str'>
print (col1[1] in col1)
False
我可能遗漏了一些东西,这是多年后的事了,但是当我读到这个问题时,您正试图让 in
关键字用于您的熊猫系列?所以可能想做:
col1[1] in col1.values
因为上面说了,pandas是通过索引来查找的,你需要特别要求它查看系列的值,而不是索引。
我遇到了这个非常烦人的问题(我是 python 的新手)
df=pd.DataFrame[{'col1':['1','2','3','4']}]
col1=df['col1']
为什么 col1[1] in col1
return False
?
检查值使用 boolean indexing
:
#get value where index is 1
print (col1[1])
2
#more common with loc
print (col1.loc[1])
2
print (col1 == '2')
0 False
1 True
2 False
3 False
Name: col1, dtype: bool
如果需要获取行:
print (col1[col1 == '2'])
1 2
Name: col1, dtype: object
用or
检查多个值:
print (col1.isin(['2', '4']))
0 False
1 True
2 False
3 True
Name: col1, dtype: bool
print (col1[col1.isin(['2', '4'])])
1 2
3 4
Name: col1, dtype: object
还有一些关于 in
的测试会员资格 docs:
Using the Python in operator on a
Series
tests for membership in the index, not membership among the values.If this behavior is surprising, keep in mind that using in on a Python dictionary tests keys, not values, and Series are dict-like. To test for membership in the values, use the method isin():
For DataFrames, likewise, in applies to the column axis, testing for membership in the list of column names.
#1 is in index
print (1 in col1)
True
#5 is not in index
print (5 in col1)
False
#string 2 is not in index
print ('2' in col1)
False
#number 2 is in index
print (2 in col1)
True
您尝试在索引值中查找字符串 2
:
print (col1[1])
2
print (type(col1[1]))
<class 'str'>
print (col1[1] in col1)
False
我可能遗漏了一些东西,这是多年后的事了,但是当我读到这个问题时,您正试图让 in
关键字用于您的熊猫系列?所以可能想做:
col1[1] in col1.values
因为上面说了,pandas是通过索引来查找的,你需要特别要求它查看系列的值,而不是索引。