Search code examples
pythonpandasnan

Fill NaN values on all columns except one (without dropping it) in dataframe


I want to fill NaN values with "Not Available" string for all columns in my dataframe except for one column (that is named "lag")

I've tried this:

#fill NaN values with "Not Available" except in "lag" column
mydataset_df[mydataset_df.columns.difference(['lag'])].fillna("Not Available", inplace = True)

And also this:

mydataset_df.loc[ : , mydataset_df.columns != 'lag'].fillna("Not Available", inplace = True)

What I get is the same result: no columns are filled with "Not Available" when blank. But if I execute:

mydataset.fillna("Not Available", inplace = True)

I get ALL the blank values in the df correctly filled with "Not Available" (so I guess the issue here is the first part in the code string when I select the columns on which the command needs to be applied)

Can somebody help me on this? I don't want to drop "lag" column, I just want to leave it and fill all the other columns if there are blanks.

Thanks in advance

Stefano


Solution

  • You can try this

    for col in mydataset_df.columns:
        if col != 'lag':
            mydataset_df[col].fillna("Not Available", inplace=True)