Python Pandas-使用字符串相等性选择数据帧中的行

Python Pandas-使用字符串相等性选择数据帧中的行,python,pandas,Python,Pandas,我试图从数据框贡献者中获取所有行,其中职业已退休,如下所示: mask = (contributors.contbr_occupation.str == 'RETIRED') print(contributors[mask]) 但是,我得到了以下堆栈跟踪: Traceback (most recent call last): File "C:\Users\Me\Anaconda3\envs\pandas\lib\site-packages\pandas\indexes\base.py",

我试图从数据框
贡献者
中获取所有行,其中职业已退休,如下所示:

mask = (contributors.contbr_occupation.str == 'RETIRED')
print(contributors[mask])
但是,我得到了以下堆栈跟踪:

Traceback (most recent call last):
  File "C:\Users\Me\Anaconda3\envs\pandas\lib\site-packages\pandas\indexes\base.py", line 2134, in get_loc
    return self._engine.get_loc(key)
  File "pandas\index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas\index.c:4433)
  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:4279)
  File "pandas\src\hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742)
  File "pandas\src\hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696)
KeyError: False

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "census_attack.py", line 27, in <module>
    print(contributors[mask])
  File "C:\Users\Me\Anaconda3\envs\pandas\lib\site-packages\pandas\core\frame.py", line 2059, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\Me\Anaconda3\envs\pandas\lib\site-packages\pandas\core\frame.py", line 2066, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\Me\Anaconda3\envs\pandas\lib\site-packages\pandas\core\generic.py", line 1386, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\Me\Anaconda3\envs\pandas\lib\site-packages\pandas\core\internals.py", line 3543, in get
    loc = self.items.get_loc(item)
  File "C:\Users\Me\Anaconda3\envs\pandas\lib\site-packages\pandas\indexes\base.py", line 2136, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas\index.c:4433)
  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:4279)
  File "pandas\src\hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742)
  File "pandas\src\hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696)
KeyError: False
回溯(最近一次呼叫最后一次):
文件“C:\Users\Me\Anaconda3\envs\pandas\lib\site packages\pandas\index\base.py”,第2134行,在get\U loc中
返回发动机。获取位置(钥匙)
pandas.index.IndexEngine.get_loc(pandas\index.c:4433)中第132行的文件“pandas\index.pyx”
文件“pandas\index.pyx”,第154行,在pandas.index.IndexEngine.get_loc(pandas\index.c:4279)中
文件“pandas\src\hashtable\u class\u helper.pxi”,第732行,位于pandas.hashtable.PyObjectHashTable.get\u项(pandas\hashtable.c:13742)中
pandas.hashtable.PyObjectHashTable.get_项(pandas\hashtable.c:13696)中的第740行文件“pandas\src\hashtable_class_helper.pxi”
KeyError:错误
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“census_attack.py”,第27行,在
打印(贡献者[掩码])
文件“C:\Users\Me\Anaconda3\envs\pandas\lib\site packages\pandas\core\frame.py”,第2059行,在\uu getitem中__
返回self.\u getitem\u列(键)
文件“C:\Users\Me\Anaconda3\envs\pandas\lib\site packages\pandas\core\frame.py”,第2066行,在\u getitem\u列中
返回self.\u获取\u项目\u缓存(密钥)
文件“C:\Users\Me\Anaconda3\envs\pandas\lib\site packages\pandas\core\generic.py”,第1386行,在\u get\u item\u缓存中
values=self.\u data.get(项目)
get中第3543行的文件“C:\Users\Me\Anaconda3\envs\pandas\lib\site packages\pandas\core\internals.py”
loc=自身项目。获取loc(项目)
文件“C:\Users\Me\Anaconda3\envs\pandas\lib\site packages\pandas\index\base.py”,第2136行,在get\U loc中
返回self.\u引擎。获取\u loc(self.\u可能\u cast\u索引器(键))
pandas.index.IndexEngine.get_loc(pandas\index.c:4433)中第132行的文件“pandas\index.pyx”
文件“pandas\index.pyx”,第154行,在pandas.index.IndexEngine.get_loc(pandas\index.c:4279)中
文件“pandas\src\hashtable\u class\u helper.pxi”,第732行,位于pandas.hashtable.PyObjectHashTable.get\u项(pandas\hashtable.c:13742)中
pandas.hashtable.PyObjectHashTable.get_项(pandas\hashtable.c:13696)中的第740行文件“pandas\src\hashtable_class_helper.pxi”
KeyError:错误

我该怎么做呢?

如果您只是在执行真正的相等性检查(而不是包含或类似的检查),不要使用
str
访问器-您不需要它

mask = (contributors.contbr_occupation == 'RETIRED')
示例

>>> df

  strings
0     abc
1     def
2     ghi
3     abc

>>> df[df.strings == 'abc']

  strings
0     abc
3     abc
如果您确实需要包含之类的逻辑条件,那么实际上可以在
str
访问器上调用string方法,例如使用

你可以用


是的,等事情平息下来,我又能有节奏了。
mask = (contributors.contbr_occupation.str.contains('RETIRED'))
contributors.query('contbr_occupation == "RETIRED"')