Esteban Madrigal
Esteban Madrigal

Reputation: 37

Replace Numeric Columns of dataset from other columns

I have a dataset that has many columns, I want to extract the numeric columns and replace with the mean of the columns and then these modified columns replacing the ones in the original dataframe.

df1 = df.select_dtypes(include = ["number"]).apply(lambda x: x.fillna(x.mean()),axis=0)
df.loc[df.select_dtypes(include = ["number"])] = df1

I managed to extract the numeric columns but I couldn't replace them, the idea is not to manually indicate which are those numeric columns.

Upvotes: 1

Views: 99

Answers (1)

fsl
fsl

Reputation: 3280

It's probably easier to assign a new/changed DataFrame. This will only change the columns you altered.

new_df = df.assign(**df.select_dtypes('number').apply(lambda x: x.fillna(x.mean())))                                                                                                                               

If you want to preserve the original DataFrame, you can do it in steps:

cols = df.select_dtypes('number').columns                                                                                                                                                  
df[cols] = df[cols].apply(lambda x: x.fillna(x.mean()))

Upvotes: 1

Related Questions