Reputation: 23
Can someone explain why 'Louisville' is returning a KeyError? It is in the data frame from what I understand. What am I missing?
Here is what the data looks like. It is a CSV.
This is what off_data.head()
looks like
off_data.index()
off_data.columns
off_data[0:2].to_dict()
Rajith Thennakoon's Suggestion
{'Conf': {'Michigan St. ': 'B10', 'Louisville ': 'ACC'},
'AdjTempo': {'Michigan St. ': 70.4, 'Louisville ': 67.8},
'AdjOE': {'Michigan St. ': 114.4, 'Louisville ': 113.9},
'eFG%': {'Michigan St. ': 52.9, 'Louisville ': 60.7},
'TO%': {'Michigan St. ': 15.9, 'Louisville ': 17.1},
'OR%': {'Michigan St. ': 37.1, 'Louisville ': 32.8},
'FTRate': {'Michigan St. ': 30.9, 'Louisville ': 32.5},
'AdjDE': {'Michigan St. ': 85.1, 'Louisville ': 87.5},
'deFG%': {'Michigan St. ': 40.3, 'Louisville ': 42.9},
'dTO%': {'Michigan St. ': 20.7, 'Louisville ': 15.9},
'dOR%': {'Michigan St. ': 25.0, 'Louisville ': 27.6},
'dFTRate': {'Michigan St. ': 27.3, 'Louisville ': 26.0}}
Input
import pandas as pd
off_data = pd.read_csv(r'C:\Users\westc\Desktop\sports.data\ncaab\kenpomdata\off20.csv', index_col= 'Team')
type(off_data)
off_data.loc["Louisville",0]
KeyError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2896 try: -> 2897 return self._engine.get_loc(key) 2898 except KeyError:
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: 'Louisville'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last) in 4 5 type(off_data) ----> 6 off_data.loc["Louisville",0]
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in getitem(self, key) 1416 except (KeyError, IndexError, AttributeError): 1417 pass -> 1418 return self._getitem_tuple(key) 1419 else: 1420 # we by definition only have the 0th axis
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup) 803 def _getitem_tuple(self, tup): 804 try: --> 805 return self._getitem_lowerdim(tup) 806 except IndexingError: 807 pass
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_lowerdim(self, tup) 927 for i, key in enumerate(tup): 928 if is_label_like(key) or isinstance(key, tuple): --> 929 section = self._getitem_axis(key, axis=i) 930 931 # we have yielded a scalar ?
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis) 1848 # fall thru to straight lookup 1849 self._validate_key(key, axis) -> 1850 return self._get_label(key, axis=axis) 1851 1852
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_label(self, label, axis) 158 raise IndexingError("no slices here, handle elsewhere") 159 --> 160 return self.obj._xs(label, axis=axis) 161 162 def _get_loc(self, key: int, axis: int):
~\Anaconda3\lib\site-packages\pandas\core\generic.py in xs(self, key, axis, level, drop_level) 3735 loc, new_index = self.index.get_loc_level(key, drop_level=drop_level) 3736
else: -> 3737 loc = self.index.get_loc(key) 3738 3739 if isinstance(loc, np.ndarray):~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2897 return self._engine.get_loc(key) 2898 `
Upvotes: 2
Views: 7218
Reputation: 7214
You can get the row by:
off_data.loc[off_data['Team'] == "Louisville"]
The way your doing the location, requires the column name, which by your output appears to be Team, you could try these to see if they work:
In [4496]: df2.loc[0,"Team"]
Out[4496]: 'Michigan'
In [4497]: df2.loc[1,"Team"]
Out[4497]: 'Louisville'
Looks like there is whitespace in the data, here's a quick way to strip the whitespace at the end:
off_data.index = off_data.index.str.strip()
which should let you do a search as is:
off_data[off_data.index == 'Louisville']
Upvotes: 1
Reputation: 4130
Try this
off_data.index = off_data.index.str.strip()
off_data.loc[off_data.index == "Louisville"]
EDIT
if you need to remove spaces when read the dataframe.you can use skipinitialspace=True.This will Skip spaces after delimiter.
df1 = pd.read_csv(.. skipinitialspace=True)
Or need to remove spaces of a particular column,you can use like this
df["column_name"] = df["column_name"].str.strip()
Or you can use pandas rstrip or lstrip as well.
lstrip,Strip whitespaces (including newlines) or a set of specified characters from each string in the Series/Index from left side.
rstrip,Strip whitespaces (including newlines) or a set of specified characters from each string in the Series/Index from right side.
Upvotes: 1