Python 无法筛选数据帧中的索引
这是我的Python 无法筛选数据帧中的索引,python,pandas,dataframe,Python,Pandas,Dataframe,这是我的pivot\u设备\u T.索引,我的数据帧是pivot\u设备 Index(['', '3', 'AXIS', ... , 'All_Bolt', 'All_Telkomsel', 'All_XL', 'All_Indosat', 'All_Smartfren'], dtype='object', name='operatorName') 我现在做的是 pivot_device_T = pivot_device_T[pivot_device_T['index'].str.
pivot\u设备\u T.索引
,我的数据帧是pivot\u设备
Index(['', '3', 'AXIS', ... , 'All_Bolt', 'All_Telkomsel', 'All_XL', 'All_Indosat', 'All_Smartfren'],
dtype='object', name='operatorName')
我现在做的是
pivot_device_T = pivot_device_T[pivot_device_T['index'].str.contains("All")]
但我得到的是
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
TypeError: an integer is required
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2441 try:
-> 2442 return self._engine.get_loc(key)
2443 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
KeyError: 'index'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
TypeError: an integer is required
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-18-aea8005e79dc> in <module>()
1 pivot_device_T = pivot_device.T
----> 2 pivot_device_T = pivot_device_T[pivot_device_T['index'].str.contains("All")]
3 #pivot_device = pivot_device_T.T
~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
1962 return self._getitem_multilevel(key)
1963 else:
-> 1964 return self._getitem_column(key)
1965
1966 def _getitem_column(self, key):
~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_column(self, key)
1969 # get column
1970 if self.columns.is_unique:
-> 1971 return self._get_item_cache(key)
1972
1973 # duplicate columns & possible reduce dimensionality
~/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
1643 res = cache.get(item)
1644 if res is None:
-> 1645 values = self._data.get(item)
1646 res = self._box_item_values(item, values)
1647 cache[item] = res
~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in get(self, item, fastpath)
3588
3589 if not isnull(item):
-> 3590 loc = self.items.get_loc(item)
3591 else:
3592 indexer = np.arange(len(self.items))[isnull(self.items)]
~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2442 return self._engine.get_loc(key)
2443 except KeyError:
-> 2444 return self._engine.get_loc(self._maybe_cast_indexer(key))
2445
2446 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
KeyError: 'index'
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
TypeError:需要一个整数
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
get_loc中的~/anaconda3/lib/python3.6/site-packages/pandas/core/index/base.py(self、key、method、tolerance)
2441请尝试:
->2442自动返回发动机。获取锁定位置(钥匙)
2443键错误除外:
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
KeyError:“索引”
在处理上述异常期间,发生了另一个异常:
TypeError回溯(最近一次调用上次)
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
TypeError:需要一个整数
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在()
1 pivot\u device\u T=pivot\u device.T
---->2 pivot_device_T=pivot_device_T[pivot_device_T['index'].str.contains(“全部”)]
3#枢轴装置=枢轴装置T.T
~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in\uuuuu getitem\uuuuu(self,key)
1962返回自我。\u获取项目\u多级(键)
1963年其他:
->1964返回自我。\u获取项目\u列(键)
1965
1966 def_getitem_列(自身,键):
~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py在_getitem_列中(self,key)
1969年#获得专栏
1970如果self.columns.u是唯一的:
->1971返回自我。获取项目缓存(密钥)
1972
1973#重复列和可能的降维
缓存中的~/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py(self,item)
1643 res=cache.get(项)
1644如果res为无:
->1645 values=self.\u data.get(项目)
1646 res=自身。_框_项_值(项,值)
1647缓存[项目]=res
get中的~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py(self、item、fastpath)
3588
3589如果不为空(项目):
->3590 loc=自身物品。获取loc(物品)
3591其他:
3592 indexer=np.arange(len(self.items))[isnull(self.items)]
get_loc中的~/anaconda3/lib/python3.6/site-packages/pandas/core/index/base.py(self、key、method、tolerance)
2442自动返回发动机。获取锁定位置(钥匙)
2443键错误除外:
->2444返回self.\u引擎。获取位置(self.\u可能\u投射\u索引器(键))
2445
2446 indexer=self.get_indexer([key],method=method,tolerance=tolerance)
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
KeyError:“索引”
使用pivot\u device\u T.index
,因为pivot\u device\u T['index']
选择列名index
,并且因为不存在它会引发错误:
pivot_device_T = pivot_device_T[pivot_device_T.index.str.contains("All")]
另一个解决办法应该是:
pivot_device_T = pivot_device_T.filter(like='All', axis=0)