I am having the same problem like paulusricky4 before.
My code:
laptops[‘weight’] = laptops[‘weight’].str.replace(‘kgs’, ‘’).str.replace(‘kg’, ‘’).astype(float)
laptops.rename({‘weight’: ‘weight_kg’}, axis=1, inplace = True)
laptops.to_csv(‘laptops_cleaned.csv’, index = False)
laptops.info()
When I ‘Run’ all looks OK. But when I 'Submit Answer’I got a long list of error:
(these are the five last lines of error messages)
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: ‘weight’
This is strange because when I type >>> laptops.info(), the output is already correct. Please help.
This annoying error means that Pandas can not find your column name in your dataframe. Before doing anything with the data frame, use print(df.columns) to see dataframe column exist or not.
print(df.columns)
I was getting a similar kind of error in one of my codes. Turns out, that particular index was missing from my data frame as I had dropped the empty dataframe 2 rows. If this is the case, you can do df.reset_index(inplace=True) and the error should be resolved.