Home > Software design >  How to remove NaN values from dataframe
How to remove NaN values from dataframe

Time:06-02

I'm trying to clean a dataset and observed few features are of type : non-null Float type. The values contain - NaN

I tried below code :

cleaned_customer_data.fillna(cleaned_customer_data.mean()).head()

This result with 0 record.

Also, i tried -

cleaned_customer_data.fillna(cleaned_customer_data.mean())

It doesn't change NaN values to mean.

Data Sample :

FEATURE1
--------
NaN
2.0
NaN
NaN
NaN
1.294

Am i doing something wrong here, please guide.

CodePudding user response:

mean_value=cleaned_customer_data['FEATURE1'].mean()
cleaned_customer_data['FEATURE1'].fillna(value=mean_value, inplace=True)
cleaned_customer_data

CodePudding user response:

first, you need to calculate the mean :

mean_df = df.loc[df['FEATURE1'].notna()]['FEATURE1'].mean()

Then you assign the value of the mean where there is a NaN:

df.loc[df['FEATURE1'].isna(),'FEATURE1'] = mean_df
  • Related