I want to fill NaN values with the means of col1 groups, but I want to keep the NaN values for 2 of the columns

Viewed 18

let's say the dataframe has 300 columns, :

col1  col2  col3.... col300
A     2     5         50
A     NaN   NaN       32
B     5     4         NaN

I want to fill in the blanks with means of corresponding groups in col1, but I want to keep col4 and col5 unchanged and actually want to keep the Nan values in those columns.

I am using the following code to fill the NaN values for the entire dataframe

df.groupby("col1").transform(lambda x: x.fillna(x.mean()))

what can I do to add the col4-col5 as exceptions?

1 Answers

You can drop col3, col4:

means = df.drop(['col3','col4'], axis=1).groupby('col1').transform('mean')
df = df.fillna(means)
Related