0

I am working on a Data set called Scania’s Air Pressure System's Failures.

The dataframe has columns with missing values.

How do I replace the zero with the mean?

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83

1 Answers1

1

Assuming that the dataframe is df, if one wants to change all the zeros throughout every column, one can use pandas.DataFrame.replace like this

df = df.replace(0, df.mean())

If one wants to do it for just one column (column_name), one can use pandas.Series.replace as follows

df['column_name'] = df['column_name'].replace(0, df['column_name'].mean())

If one has NaN, one can use pandas.DataFrame.fillna as follows

df = df.fillna(df.mean())

If the NaN are just in the column column_name, one can use pandas.Series.fillna as follows

df['column_name'] = df['column_name'].fillna(df['column_name'].mean())
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83