Mert Karagöz
Mert Karagöz

Reputation: 71

Why isn't this replace in DataFrame doing what I intended?

I'm trying to replace NaN in train_df with values of corresponding indexes in dff. I can't understand what I'm doing wrong.

train_df.replace(to_replace = train_df["Age"].values , 
                 value = dff["Age"].values , 
                 inplace = True , 
                 regex = False , 
                 limit = None)

dff.Age.mean()

Output : 30.128401985359698

train_df.Age.mean()

Output : 28.96758312013303

Upvotes: 1

Views: 85

Answers (1)

L3n95
L3n95

Reputation: 1615

You replace everything in train_df not just NaN.

The replace docs say:

Replace values given in to_replace with value.

If you just want to replace the NaN you should take a look at fillna or maybe you could use indexing with isna.

fillna Docs

isna Docs

Example with fillna

df1 = pd.DataFrame({"a": [1, 2, np.nan, 4]})
df2 = pd.DataFrame({"a": [5, 5, 3, 5]})
df1.fillna(df2, inplace=True)

Example with isna

df1[pd.isna(df1)] = df2

Results

>> df1
   a
0  1.0
1  2.0
2  3.0
3  4.0

Upvotes: 2

Related Questions