df.loc['l1', 'c2']
Quando utilizo a função no 'loc' no Jupyter Notebook para localizar uma variável no DataFrame está apresentando este erro:
Se utilizo a função 'iloc' não apresenta erro.
KeyError Traceback (most recent call last) ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2894 try: -> 2895 return self.engine.getloc(casted_key) 2896 except KeyError as err:
pandas_libs\index.pyx in pandas.libs.index.IndexEngine.getloc()
pandas_libs\index.pyx in pandas.libs.index.IndexEngine.getloc()
pandas_libs\hashtable_class_helper.pxi in pandas.libs.hashtable.PyObjectHashTable.getitem()
pandas_libs\hashtable_class_helper.pxi in pandas.libs.hashtable.PyObjectHashTable.getitem()
KeyError: 'l1'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last) in ----> 1 df.loc['l1', 'c2']
~\anaconda3\lib\site-packages\pandas\core\indexing.py in getitem(self, key) 871 # AttributeError for IntervalTree get_value 872 pass --> 873 return self.getitemtuple(key) 874 else: 875 # we by definition only have the 0th axis
~\anaconda3\lib\site-packages\pandas\core\indexing.py in getitemtuple(self, tup) 1042 def getitemtuple(self, tup: Tuple): 1043 try: -> 1044 return self.getitemlowerdim(tup) 1045 except IndexingError: 1046 pass
~\anaconda3\lib\site-packages\pandas\core\indexing.py in getitemlowerdim(self, tup) 784 # We don't need to check for tuples here because those are 785 # caught by the isnested_tuple_indexer check above. --> 786 section = self.getitemaxis(key, axis=i) 787 788 # We should never have a scalar section here, because
~\anaconda3\lib\site-packages\pandas\core\indexing.py in getitemaxis(self, key, axis) 1108 # fall thru to straight lookup 1109 self.validatekey(key, axis) -> 1110 return self.getlabel(key, axis=axis) 1111 1112 def getslice_axis(self, slice_obj: slice, axis: int):
~\anaconda3\lib\site-packages\pandas\core\indexing.py in getlabel(self, label, axis) 1057 def getlabel(self, label, axis: int): 1058 # GH#5667 this will fail if the label is not present in the axis. -> 1059 return self.obj.xs(label, axis=axis) 1060 1061 def handlelowerdim_multi_index_axis0(self, tup: Tuple):
~\anaconda3\lib\site-packages\pandas\core\generic.py in xs(self, key, axis, level, drop_level) 3489 loc, new_index = self.index.get_loc_level(key, drop_level=drop_level) 3490 else: -> 3491 loc = self.index.get_loc(key) 3492 3493 if isinstance(loc, np.ndarray):
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2895 return self.engine.getloc(casted_key) 2896 except KeyError as err: -> 2897 raise KeyError(key) from err 2898 2899 if tolerance is not None:
KeyError: 'l1'