Error when trying to submit. KeyError: 'weight'

Screen Link:

My Code:

laptops["weight"] = laptops["weight"].str.replace("kg", "").str.replace("s","").astype(float)
laptops = laptops.rename(columns={"weight":"weight_kg"})
laptops.to_csv("laptops_cleaned.csv", index=False)

What I expected to happen: there should be no problem with my code

What actually happened:

Running your code caused an error.

but I can’t find where the problem is

Hi @paulusricky4, welcome to the community!

I run your code and there is no problem - see below:


1 Like

Your code works fine on my computer as well.

1 Like

I tried to resubmit and it worked … I think it’s just a technical error … Thank you all

1 Like

Thanks, it is working now. seems like tech error.

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)
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 >>>, the output is already correct. Please help.

Your code is working fine on my end. Try refreshing the page or leaving the mission before trying again.

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.


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.