简体   繁体   中英

Why am I receiving a key error using .loc on a known observation

I'm getting a key error when trying to pull a row of my data by index. I'm choosing the index based off looking at the .head() command so I know the value is there. My index is based off of a policy id.

I've looked at a few posts like the following but they don't answer the question: KeyError in Python Pandas dataframe using.loc

Here is a screenshot of the data frame: 在此处输入图像描述

Then I try:

df_test.loc[92026963:]

This gives me a key error. Then I tried:

df_test.loc[92026963]

This gave me the same error. I even tried putting the policy id in quotes. Full traceback below:

ValueError Traceback (most recent call last) C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_slice_bound(self, label, side, kind) 4804 try: -> 4805 return self._searchsorted_monotonic(label, side) 4806 except ValueError:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in _searchsorted_monotonic(self, label, side) 4764 -> 4765 raise ValueError('index must be monotonic increasing or decreasing') 4766

ValueError: index must be monotonic increasing or decreasing

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-45-85aa3a940778> in <module>
----> 1 df_test.loc[92026963:]

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1498 
   1499             maybe_callable = com.apply_if_callable(key, self.obj)
-> 1500             return self._getitem_axis(maybe_callable, axis=axis)
   1501 
   1502     def _is_scalar_access(self, key):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1865         if isinstance(key, slice):
   1866             self._validate_key(key, axis)
-> 1867             return self._get_slice_axis(key, axis=axis)
   1868         elif com.is_bool_indexer(key):
   1869             return self._getbool_axis(key, axis=axis)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_slice_axis(self, slice_obj, axis)
   1531         labels = obj._get_axis(axis)
   1532         indexer = labels.slice_indexer(slice_obj.start, slice_obj.stop,
-> 1533                                        slice_obj.step, kind=self.name)
   1534 
   1535         if isinstance(indexer, slice):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in slice_indexer(self, start, end, step, kind)
   4671         """
   4672         start_slice, end_slice = self.slice_locs(start, end, step=step,
-> 4673                                                  kind=kind)
   4674 
   4675         # return a slice

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in slice_locs(self, start, end, step, kind)
   4870         start_slice = None
   4871         if start is not None:
-> 4872             start_slice = self.get_slice_bound(start, 'left', kind)
   4873         if start_slice is None:
   4874             start_slice = 0

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_slice_bound(self, label, side, kind)
   4806             except ValueError:
   4807                 # raise the original KeyError
-> 4808                 raise err
   4809 
   4810         if isinstance(slc, np.ndarray):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_slice_bound(self, label, side, kind)
   4800         # we need to look up the label
   4801         try:
-> 4802             slc = self._get_loc_only_exact_matches(label)
   4803         except KeyError as err:
   4804             try:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in _get_loc_only_exact_matches(self, key)
   4770         get_slice_bound.
   4771         """
-> 4772         return self.get_loc(key)
   4773 
   4774     def get_slice_bound(self, label, side, kind):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2657                 return self._engine.get_loc(key)
   2658             except KeyError:
-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2661         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 92026963

It seems here is necessary assign back DataFrame.set_index :

df_test = df_test.set_index('policy')

Or use inplace=True :

df_test.set_index('policy', inplace=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM