I have a dataset with 10 columns. Among them I want to change the '0' values of 8 columns with mode methods. 0 is not only the values of those columns, there are other values too. Among them I only want change the 0 values with mode. Suppose the dataset looks like this,
col1 col2 col3 col4 col5 col6 col7 col8 col9 col10
I want to change the '0' values from col3 to col10 at a time.
I used the following code to change 0 to mode
replace_col = df['col3']
replace_col.replace(to_replace = 0, value = replace_col.mode()[0], inplace=True)
However, I need to change the values of df, such as df['col3'], df['col4'], df['col4'] each time. So I need to run the code 8 times to change the values of 8 columns. Is there any way I can change the value at a time running one code snippet?
Thank you.