Search code examples
pythondataframedata-analysismissing-dataexploratory-data-analysis

Python function to identify 0 as a missing value in numerical columns of a dataframe


I need to write a function where zero in a numeric column is identified as missing value in python. Can anyone help me with how to do that?

#numerical columns in df dataframe
df_num = df.select_dtypes(exclude='object')

After this I need a function to convert all zeroes in df_num to be picked as missing values.

This is what I tried

df_num.replace(0,np.nan)

But the dataset is not being changed.

Is there any other way I can do it?


Solution

  • What you would be missing there is to assign it to the column or columns from which it is expected to convert the 0s into missing values

    df_num['numeric_column'] = df_num['numeric_column'].replace(0, np.nan)
    

    And finally, display the dataframe to see the changes with print(df_num).
    I hope it helps, greetings!