Dataset columns throwing KeyError

0

1

For some reason, the columns in my dataset are throwing the following KeyError:

Traceback (most recent call last):
  File "C:\Users\security\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py", line 2657, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'FirePlaceQu'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/security/Downloads/AP/Boston-Kaggle/Boston.py", line 29, in <module>
    train['FirePlaceQu'] = train['FirePlaceQu'].fillna('NFp')
  File "C:\Users\security\AppData\Roaming\Python\Python37\site-packages\pandas\core\frame.py", line 2927, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\security\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py", line 2659, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'FirePlaceQu'

I couldn't decipher what the error was about. It gave the exact same error on other columns as well so it wasn't just FirePlaceQu. I double checked the spelling so it isn't that either.

This is my code:

import pandas as pd

train = pd.read_csv("https://raw.githubusercontent.com/oo92/Boston-Kaggle/master/train.csv")
test = pd.read_csv("https://raw.githubusercontent.com/oo92/Boston-Kaggle/master/test.csv")

categorical_columns = ['MSSubCLass', 'MSZoning', 'LotShape', 'LandContour', 'LotConfig', 'Neighborhood', 'Condition1',
                       'Condition2', 'BldgType', 'HouseStyle', 'RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd',
                       'Foundation', 'Heating', 'Electrical', 'Functional', 'Fireplaces', 'GarageType', 'GarageYrBlt',
                       'GarageCars', 'GarageArea', 'PavedDrive', 'Fence', 'MiscFeature', 'SaleType', 'SaleCondition']

ranked_columns = ['LotArea', 'Utilities', 'LandSlope', 'ExterQual', 'ExterCond', 'BsmtQual', 'BsmtCond', 'BsmtExposure',
                  'BsmtFinType1', 'BsmtFinType2', 'HeatingQC', 'KitchenQual', 'FireplaceQu', 'GarageQual', 'GarageCond',
                  'PoolQC', 'OverallQual', 'OverallCond']

numerical_columns = ['LotFrontage', 'YearBuilt', 'YearRemodAdd', 'MasVnrArea', 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF',
                     'TotalBsmtSF', '1stFlrSF', '2ndFlrSf', 'LowQualFinSF', 'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath',
                     'FullBath', 'HalfBath', 'Bedroom', 'Kitchen', 'TotRmsAbvGrd', 'Fireplaces', 'GarageYrBlt', 'GarageCars',
                     'GarageArea', 'WoodDeckSF', 'OpenPorchSF', 'EnclosedPorch', '3SsnPorch', 'SceenPorch', 'PoolArea',
                     'MiscVal', 'MoSold', 'YrSold']

binary_columns = ['Street', 'CentralAir']

train['Alley'] = train['Alley'].fillna('NoAy')
train['BsmtQual'] = train['BsmtQual'].fillna('NBs')
train['BsmtCond'] = train['BsmtCond'].fillna('NBs')
train['BsmtExposure'] = train['BsmtExposure'].fillna('NBs')
train['BsmtFinType1'] = train['BsmtFinType1'].fillna('NBs')
train['BsmtFinType2'] = train['BsmtFinType2'].fillna('NBs')
train['FirePlaceQu'] = train['FirePlaceQu'].fillna('NFp')
train['GarageType'] = train['GarageType'].fillna('NGr')
train['GarageFinish'] = train['GarageFinish'].fillna('NGr')
train['GarageQual'] = train['GarageQual'].fillna('NGr')
train['GarageCond'] = train['GarageCond'].fillna('NGr')
train['PoolQC'] = train['PoolQC'].fillna('NPo')
train['Fence'] = train['Fence'].fillna('NFn')
train['MiscFeature'] = train['MiscFeature'].fillna('NoN')

train['Utilities'] = train['Utilities'].replace(['AllPub', 'NoSeWa'], [2, 1])  # Utilities
train['ExterQual'] = train['ExterQual'].replace(['Ex', 'Gd', 'TA', 'Fa'], [4, 3, 2, 1])  # Exterior Quality
train['LandSlope'] = train['LandSlope'].replace(['Gtl', 'Mod', 'Sev'], [3, 2, 1])  # Land Slope
train['ExterCond'] = train['ExterCond'].replace(['Ex', 'Gd', 'TA', 'Fa', 'Po'], [4, 3, 2, 1, 0])  # Exterior Condition
train['HeatingQC'] = train['HeatingQC'].replace(['Ex', 'Gd', 'TA', 'Fa', 'Po'], [4, 3, 2, 1, 0])  # Heating Quality and Condition
train['KitchenQual'] = train['KitchenQual'].replace(['Ex', 'Gd', 'TA', 'Fa'], [3, 2, 1, 0])  # Kitchen Quality

Andros Adrianopolos

Posted 2019-07-02T07:18:18.210

Reputation: 322

Answers

3

It's FireplaceQu not FirePlaceQu .

A KeyError means it doesn't find the column in the dataset.

to simply check the spelling you can print all the columns:

for col_name in train:
    print(col_name)

or check if your spelling of a column is correct:

"FirePlaceQu" in list(map(lambda x: x, train))

vico

Posted 2019-07-02T07:18:18.210

Reputation: 138

Oops. That's embarrassing. Thanks a lot for noticing. – Andros Adrianopolos – 2019-07-02T08:09:31.870