Search code examples
pythonpandasdataframefillna

Return the changed values after using fillna() python's pandas


I am using fillna() in a dataframe to change the null values from one column for another ones from another column, but I don't seem to find the way to print the changed values. I've tried to iterate over fillna() but that doesn't do the trick.

df.column_with_nan.fillna(column_to_use_as_nan_replacement, inplace=True)

I was thinking maybe I could run the new changed column against the old unchanged column, but I'd like to know whether there's a simpler way.

Any piece of advice?

Thanks!


Solution

  • I don't know of any non-hacky way to straight print the changed values, but you can use pandas.Series.compare() to get the changed values. Just do

    tmp = pd.Series(df1.column_with_nan.copy())
    df.row_with_nan.fillna(column_to_use_as_nan_replacement, inplace=True)
    print(tmp.compare(df.column_with_nan))
    

    You can also run df.compare for full dataframes.