karmilah ila
karmilah ila

Reputation: 1

Python for imputing missing values with mean, help me I try this code but error

df.loc[(data['Perkotaan'].isnull()==True),'Perkotaan']=df['Perkotaan'].mean()
df.loc[(data['Perdesaan'].isnull()==True),'Perdesaan']=df['Perdesaan'].mean()

KeyError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2897 try: -> 2898 return self._engine.get_loc(casted_key) 2899 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: 'Perkotaan'

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last) 2 frames /usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2898 return self._engine.get_loc(casted_key) 2899 except KeyError as err: -> 2900 raise KeyError(key) from err 2901 2902 if tolerance is not None:

KeyError: 'Perkotaan'

Upvotes: 0

Views: 224

Answers (1)

tdbui1209
tdbui1209

Reputation: 1

your dataframe assigned to df or data?

df.loc[(data['Perkotaan'].isnull()==True),'Perkotaan']=df['Perkotaan'].mean()

I think you must change it to df or double-check your selected column. 'Perkotaan' is not a column of your dataframe.

Upvotes: 0

Related Questions