Python 3.x 熊猫:查找给定列包含特定子字符串的行
我有以下代码,试图找到Python 3.x 熊猫:查找给定列包含特定子字符串的行,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我有以下代码,试图找到my_df中的行,其中column_A中的值包含子字符串“abc” my_df['abc' in my_df.column_A] 但我得到了以下错误: KeyError Traceback (most recent call last) <ipython-input-37-9aaddc5e75d8> in <module>() 7 8 ----> 9
my_df
中的行,其中column_A
中的值包含子字符串“abc”
my_df['abc' in my_df.column_A]
但我得到了以下错误:
KeyError Traceback (most recent call last)
<ipython-input-37-9aaddc5e75d8> in <module>()
7
8
----> 9 my_df['abc' in my_df.column_A]
/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in __getitem__(self, key)
2057 return self._getitem_multilevel(key)
2058 else:
-> 2059 return self._getitem_column(key)
2060
2061 def _getitem_column(self, key):
/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in _getitem_column(self, key)
2064 # get column
2065 if self.columns.is_unique:
-> 2066 return self._get_item_cache(key)
2067
2068 # duplicate columns & possible reduce dimensionality
/usr/local/lib/python3.4/dist-packages/pandas/core/generic.py in _get_item_cache(self, item)
1384 res = cache.get(item)
1385 if res is None:
-> 1386 values = self._data.get(item)
1387 res = self._box_item_values(item, values)
1388 cache[item] = res
/usr/local/lib/python3.4/dist-packages/pandas/core/internals.py in get(self, item, fastpath)
3539
3540 if not isnull(item):
-> 3541 loc = self.items.get_loc(item)
3542 else:
3543 indexer = np.arange(len(self.items))[isnull(self.items)]
/usr/local/lib/python3.4/dist-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
2134 return self._engine.get_loc(key)
2135 except KeyError:
-> 2136 return self._engine.get_loc(self._maybe_cast_indexer(key))
2137
2138 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4164)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4028)()
pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13166)()
pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13120)()
KeyError: False
keyrerror回溯(最近一次调用)
在()
7.
8.
---->9 my_df[“my_df.column_A”中的“abc”]
/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in_u__获取项目(self,key)
2057返回自我。\u获取项目\u多级(键)
2058其他:
->2059返回自我。\u获取项目\u列(键)
2060
2061 def_getitem_列(自身,键):
/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py在_getitem_列中(self,key)
2064#获取列
2065如果self.columns.u是唯一的:
->2066返回自我。\u获取\u项目\u缓存(密钥)
2067
2068#重复列和可能的降维
/缓存中的usr/local/lib/python3.4/dist-packages/pandas/core/generic.py(self,item)
1384 res=cache.get(项)
1385如果res为无:
->1386 values=self.\u data.get(项目)
1387 res=自身。方框\项目\值(项目,值)
1388缓存[项目]=res
/get中的usr/local/lib/python3.4/dist-packages/pandas/core/internals.py(self、item、fastpath)
3539
3540如果不为空(项目):
->3541 loc=自身物品。获取物品位置(物品)
3542其他:
3543 indexer=np.arange(len(self.items))[isnull(self.items)]
/get_loc中的usr/local/lib/python3.4/dist-packages/pandas/index/base.py(self、key、method、tolerance)
2134返回发动机。获取位置(钥匙)
2135键错误除外:
->2136返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
2137
2138 indexer=self.get\u indexer([key],method=method,tolerance=tolerance)
pandas.index.IndexEngine.get_loc中的pandas/index.pyx(pandas/index.c:4164)()
pandas/index.pyx在pandas.index.IndexEngine.get_loc(pandas/index.c:4028)()
pandas.hashtable.PyObjectHashTable.get_项中的pandas/src/hashtable_class_helper.pxi(pandas/hashtable.c:13166)()
pandas.hashtable.PyObjectHashTable.get_项中的pandas/src/hashtable_class_helper.pxi(pandas/hashtable.c:13120)()
KeyError:错误
你知道怎么解决这个问题吗?谢谢 这将返回所需的行
my_df[my_df['column_A'].str.contains('abc')]