Reputation: 3357
I have a DataFrame
with some NaN values, that I want to fit with the mean of my DataFrame
data.fillna(data.mean())
afterwards, I check to see if there are any NaN or null values left
print('is nan', data.isna().values.any()) #prints True
print('is null', data.isnull().values.any()) #prints True
before prints to be true, and I don't understand since the NaN values should be filled.
Am I missing something?
Upvotes: 1
Views: 1034
Reputation: 2096
This is a sample reproducible example.
import pandas as pd
# generate sample dataframe some filled with NaN
data = pd.DataFrame([[1, 2], [np.nan, 4], [5, 6], [7, np.nan]], columns=["A", "B"])
# apply per-column and substitue the mean of that columns
data.apply(lambda x: x.fillna(x.mean()),axis=1)
Upvotes: 1
Reputation: 10224
Try this to change your dataframe in place:
data.fillna(data.mean(), inplace=True)
Or do this to create a new dataframe with NaNs replaced by data.mean()
:
data_new = data.fillna(data.mean())
Upvotes: 2