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)
The .replace method can work in 2 ways:
.replace
Return the new, changed, dataframe.
Do the change in-place and return None.
None
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)
or:
df['COL C'].replace('', np.nan, inplace=True)