简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM