简体   繁体   中英

Fill NaN values in a column w/ column average

I have the following dataframe:

df

I would like to replace the NaN value in the purch_amt column with the column mean. I have the following code:

df2= df['purch_amt'].fillna(value= df['purch_amt'].mean()).to_frame()
df2

However this is returning only the purch_amt column as a dataframe. Ideally I would like to output the entire dataframe, with the updated purch_amt column.

Never mind I answered my own question. Correct code should be:

df['purch_amt'].fillna(value= df['purch_amt'].mean()).to_frame()
df

This returns the entire original dataframe, with the puch_amt column mean replacing it's NaN value

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