Key Error After Reading Csv Into Pandas
KeyError Pandas – How To Fix
Pandas KeyError is frustrating. This mistake happens because Pandas cannot find what you lot're looking for.
To fix this either:
- Preferred Option: Make sure that your column label (or row label) is in your dataframe!
- Error catch option: Utilise df.get('your cavalcade') to look for your cavalcade value. No error will be thrown if it is non found.
ane. df.go('your_column', default=value_if_no_column)
Pseudo code: Check to see if a column is in your dataframe, if non, return the default value.
Pandas KeyError
In most cases, think of 'key' as the aforementioned as 'proper name.' Pandas is telling you that information technology can not find your column name. The preferred method is to *make sure your column name is in your dataframe.*
OR if you want to try and catch your mistake, you tin can use df.go('your_column'). However, if yous don't know what columns are in your dataframe…practice you really know your data?
It's best to head back upstream with your code and debug where your expectations and dataframe columns mismatch.
Endeavour, Except
For a general solution, you lot can use the Attempt Except
convention to catch errors in your code. Withal, beware. Using a coating Endeavor/Except clause is dangerous and poor code practice if you lot do non know what you are doing.
If you are 'communicable' full general errors with try/except, this ways that anything can slip through your code. This could result in unexpected errors getting through and a spider web of complexity.
Goal: Try to never allow the reality of your code go too far away from the expectations of your lawmaking.
Let'due south accept a look at a sample:
Pandas KeyError¶
Pandas KeyError can exist annoying. It by and large happens when pandas cannot notice the affair yous're looking for. Commonly this is to due a column information technology cannot find. It'south simple to debug!
Let'due south check out some examples:
- Locating the error
- Fixing the error via the root crusade
- Catching the error with df.get()
Commencement, permit's create a DataFrame
In [5]:
df = pd . DataFrame ([( 'Foreign Picture palace' , 'Restaurant' ), ( 'Liho Liho' , 'Restaurant' ), ( '500 Club' , 'bar' ), ( 'The Square' , 'bar' )], columns = ( 'name' , 'blazon' ) ) df
Out[5]:
Now permit's try to call a column that is in our dataframe and is Not in our dataframe
In [6]:
# Is in our dataframe df [ 'name' ]
Out[6]:
In [9]:
# Is non in our dataframe df [ 'nutrient' ]
--------------------------------------------------------------------------- KeyError Traceback (most contempo telephone call last) /opt/anaconda3/lib/python3.seven/site-packages/pandas/cadre/indexes/base.py in get_loc (self, key, method, tolerance) 2888 endeavor : -> 2889 return self._engine.get_loc(casted_key) 2890 except KeyError as err: 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.PyObjectHashTable.get_item () pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item () KeyError: 'nutrient' The above exception was the straight cause of the following exception: KeyError Traceback (near recent telephone call last) <ipython-input-9-dce7ca21d87e> in <module> 1 # Is not in our dataframe ----> 2 df[ 'food' ] /opt/anaconda3/lib/python3.7/site-packages/pandas/cadre/frame.py in __getitem__ (self, key) 2897 if self.columns.nlevels > ane : 2898 return self._getitem_multilevel(cardinal) -> 2899 indexer = cocky.columns.get_loc(key) 2900 if is_integer(indexer) : 2901 indexer = [indexer] /opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base of operations.py in get_loc (cocky, central, method, tolerance) 2889 return self._engine.get_loc(casted_key) 2890 except KeyError as err: -> 2891 heighten KeyError(key) from err 2892 2893 if tolerance is not None : KeyError: 'nutrient'
Oh no! We got a KeyError. This means that Pandas cannot find "food" within our dataframe. We know why this is. Simply, it's not in our DF.
To become effectually this, either add together a 'food' column. Or use df.get() to try and take hold of it. Hither nosotros will use .get() and notice there is no error thrown.
This mistake can also happen when yous're trying to admission an index (for rows) label that doesn't exist. Check out this instance
Out[22]:
--------------------------------------------------------------------------- ValueError Traceback (most contempo call last) /opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/range.py in get_loc (self, key, method, tolerance) 350 try : --> 351 render self._range.index(new_key) 352 except ValueError as err: ValueError: nine is not in range The above exception was the directly crusade of the following exception: KeyError Traceback (most recent call final) <ipython-input-23-026debeea1cf> in <module> one # Does not ----> 2 df.loc[ 9 ] /opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in __getitem__ (self, fundamental) 877 878 maybe_callable = com.apply_if_callable(key, cocky.obj) --> 879 return self._getitem_axis(maybe_callable, axis=centrality) 880 881 def _is_scalar_access(self, key: Tuple) : /opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in _getitem_axis (self, central, axis) 1108 # fall thru to straight lookup 1109 self._validate_key(key, centrality) -> 1110 return cocky._get_label(key, axis=axis) 1111 1112 def _get_slice_axis(cocky, slice_obj: slice, centrality: int) : /opt/anaconda3/lib/python3.seven/site-packages/pandas/cadre/indexing.py in _get_label (self, characterization, axis) 1057 def _get_label(self, characterization, axis: int) : 1058 # GH#5667 this will fail if the characterization is non present in the axis. -> 1059 render self.obj.xs(characterization, axis=axis) 1060 1061 def _handle_lowerdim_multi_index_axis0(self, tup: Tuple) : /opt/anaconda3/lib/python3.vii/site-packages/pandas/core/generic.py in xs (self, key, centrality, level, drop_level) 3480 loc, new_index = cocky.index.get_loc_level(cardinal, drop_level=drop_level) 3481 else : -> 3482 loc = self.index.get_loc(central) 3483 3484 if isinstance(loc, np.ndarray) : /opt/anaconda3/lib/python3.seven/site-packages/pandas/core/indexes/range.py in get_loc (cocky, key, method, tolerance) 351 return self._range.index(new_key) 352 except ValueError as err: --> 353 heighten KeyError(key) from err 354 enhance KeyError(key) 355 return super( ) .get_loc(key, method=method, tolerance=tolerance) KeyError: ix
This example immediately jumps out to me and says "hey, I tin can not observe the characterization 9 in your row. Do something well-nigh this"
If you wanted to programatically do this, the long way would be to check for it first. If it is in the index, so proceed. But ordinarily, I want to brand sure I'chiliad calling something I KNOW is in my index.
In [25]:
value = 9 if value in df . index : impress ( df . loc [ value ]) else : print ( "Not in alphabetize" )
In [26]:
value = 2 if value in df . alphabetize : print ( df . loc [ value ]) else : print ( "Not in index" )
proper name 500 Lodge type bar Name: 2, dtype: object
Cheque out more than Pandas functions on our Pandas Page
You May Also Like
User Memory – How To Manually Summate
Learn more
TypeError Pandas Missing Statement – How to fix
Learn more than
Selecting Data – Pandas loc & iloc[] – The Guide
Larn more
Source: https://dataindependent.com/pandas/keyerror-pandas-how-to-fix/
0 Response to "Key Error After Reading Csv Into Pandas"
Post a Comment