I have a column (Col C) filled with some values randomly and so for the cells with missing (blank) data I want to fill them with NAN. My line of code should do that but instead it wipes out the whole column with blanks. What am I doing wrong?
df[‘COL C’] = df[‘COL C’].replace(‘’, np.nan, inplace = True)
.replace method can work in 2 ways:
Return the new, changed, dataframe.
Do the change in-place and return
You’re doing the change in-place and then storing the returned result (
None) back into the dataframe.
You should do either:
df['COL C'] = df['COL C'].replace('', np.nan)
df['COL C'].replace('', np.nan, inplace=True)