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!
CodePudding user response:
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.
CodePudding user response:
You could create a mask before replacing the values:
mask = df["col_with_nan"].isna()
After replacing the nan values, you can then access the changed rows like this:
df[mask]
Example:
import pandas as pd
import numpy as np
df = pd.DataFrame(
{
"col_with_nan": [np.nan, 1, 1, 1, np.nan, 1, 1, 1, np.nan],
"column_to_use_as_nan_replacement": [11, 22, 33, 44, 55, 66, 77, 88, 99],
}
)
print(df)
mask = df["col_with_nan"].isna()
df.col_with_nan.fillna(df.column_to_use_as_nan_replacement, inplace=True)
print(df)
print(df[mask])