I\'d like to replace bad values in a column of a dataframe by NaN\'s.
mydata = {\'x\' : [10, 50, 18, 32, 47, 20], \'y\' : [\'12\', \'11\', \'N/A\', \'13\', \
You can use replace:
df['y'] = df['y'].replace({'N/A': np.nan})
Also be aware of the inplace
parameter for replace
. You can do something like:
df.replace({'N/A': np.nan}, inplace=True)
This will replace all instances in the df without creating a copy.
Similarly, if you run into other types of unknown values such as empty string or None value:
df['y'] = df['y'].replace({'': np.nan})
df['y'] = df['y'].replace({None: np.nan})
Reference: Pandas Latest - Replace