Home > Net >  Replace Numeric Columns of dataset from other columns
Replace Numeric Columns of dataset from other columns

Time:02-08

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.

CodePudding user response:

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()))
  •  Tags:  
  • Related