Home > Blockchain >  Replacing NaN values on dataframe
Replacing NaN values on dataframe

Time:10-26

I have a df with some NaN values and I want to replace them with the mean of the values on the adjacents columns (at the same line).

How can I do it?

I'm trying to iterate over all the elements of the dataframe but I'm not going anywhere. Can someone please help me?

CodePudding user response:

Use interpolate with the default linear method:

df2 = df.interpolate(axis=1)

CodePudding user response:

A good way to do this would be:

mean = df["columnName"].mean()
df["columnName"].fillna(mean,inplace=True)

The inplace=True modifies the original dataframe.

EDIT: For a justification on why to not use inplace=True, you can find it here: Why You Should Probably Never Use pandas inplace=True

  • Related