0
$\begingroup$

I need to replace a specific value in a particular column with NaN. Here is what I tried but it didn't work.

# Names of the columns we're searching for missing values 
columns = ['median', 'p25th', 'p75th']

# Take a look at the dtypes
print(recent_grads.columns.dtype)

# Find how missing values are represented
print(recent_grads["median"].unique())
print(recent_grads["p25th"].unique())
print(recent_grads["p75th"].unique())

# Replace missing values with NaN
for column in columns:
    recent_grads.loc[recent_grads.columns == 'UN', column] = np.nan
$\endgroup$

1 Answer 1

1
$\begingroup$

If I understood your question correctly, you can do something like:

import numpy as np

columns = ['median', 'p25th', 'p75th']
df[columns] = df[columns].replace(r'UN', np.nan, regex=True)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.