You can replace this just for that column using replace:

df['workclass'].replace('?', np.NaN)

or for the whole df:

df.replace('?', np.NaN)

UPDATE

OK I figured out your problem, by default if you don't pass a separator character then read_csv will use commas ',' as the separator.

Your data and in particular one example where you have a problematic line:

54, ?, 180211, Some-college, 10, Married-civ-spouse, ?, Husband, Asian-Pac-Islander, Male, 0, 0, 60, South, >50K

has in fact a comma and a space as the separator so when you passed the na_value=['?'] this didn't match because all your values have a space character in front of them all which you can't observe.

if you change your line to this:

rawfile = pd.read_csv(filename, header=None, names=DataLabels, sep=',\s', na_values=["?"])

then you should find that it all works:

27      54               NaN  180211  Some-college             10 
Answer from EdChum on Stack Overflow
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.replace.html
pandas.DataFrame.replace โ€” pandas 3.0.2 documentation
For example, {'a': 1, 'b': 'z'} looks for the value 1 in column โ€˜aโ€™ and the value โ€˜zโ€™ in column โ€˜bโ€™ and replaces these values with whatever is specified in value. The value parameter should not be None in this case. You can treat this as a special case of passing two lists except that you are specifying the column to search in. For a DataFrame nested dictionaries, e.g., {'a': {'b': np.nan}}, are read as follows: look in column โ€˜aโ€™ for the value โ€˜bโ€™ and replace it with NaN.
Discussions

python - How to replace NaN values in a dataframe column - Stack Overflow
I agree that fillna is the canonical ... to other pandas methods, namely where. 2024-04-04T15:27:07.59Z+00:00 ... Sorry I missed typed earlier, I meant - < df = df.replace(np.nan, 0) >> 2024-04-04T18:11:53.107Z+00:00 ... Using lambda expression, it is also possible to replace NaN with ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
I need to replace NaN in one column with value for other col
I've seen this come up before. You want to use np.where. data['Grade'] = np.where(data['Grade'].isna(),data['Score'],data['Grade']) here's an example that sets null values in grade to values in score, and if it's not null, leaves the current value. More on reddit.com
๐ŸŒ r/learnpython
10
1
July 15, 2021
python - Replace invalid values with None in Pandas DataFrame - Stack Overflow
You can use df.replace('pre', 'post') ... want to replace with None value, which if you try, you get a strange result. ... Since I want to pour this data frame into MySQL database, I can't put NaN values into any element in my data frame and instead want to put None. Surely, you can first change '-' to NaN and then convert NaN to None, but I want to know why the dataframe acts in such a terrible way. Tested on pandas 0.12.0 dev ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to replace a range of values with NaN in Pandas data-frame? - Stack Overflow
I have a huge data-frame. How should I replace a range of values (-200, -100) with NaN? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.fillna.html
pandas.DataFrame.fillna โ€” pandas 3.0.2 documentation
Only replace the first NaN element. >>> df.fillna(value=values, limit=1) A B C D 0 0.0 2.0 2.0 0.0 1 3.0 4.0 NaN 1.0 2 NaN 1.0 NaN 3.0 3 NaN 3.0 NaN 4.0
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ i need to replace nan in one column with value for other col
r/learnpython on Reddit: I need to replace NaN in one column with value for other col
July 15, 2021 -

I've been working on learning Python and for something to code, I picked some VBA that I had.

In VBA:

     If Cells(I, "C").Value <> "" And Cells(I, "B").Value = "" Then
       Cells(I, "B").Value = Cells(I, "C").Value
     End If

It simply checks if colC is not Null and colB is Null, then replaces colB with the value from colC.

I can read in the csv file, I was able to select and delete some rows I didn't want, but I can't seem to get the syntax right for this...

Find elsewhere
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-replace-a-string-value-with-nan-in-pandas-data-frame-python
How to Replace a String Value with NaN in Pandas Data Frame Python | Saturn Cloud Blog
November 14, 2023 - We can replace a string value with NaN in Pandas data frame using the replace() method. The replace() method takes a dictionary of values to be replaced as keys and their corresponding replacement values as values.
๐ŸŒ
Moonbooks
moonbooks.org โ€บ Articles โ€บ How-to-replace-NaN-values-in-a-pandas-dataframe-
How to replace NaN values in a pandas dataframe ?
August 25, 2022 - import pandas as pd import numpy as np data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve'], 'Age':[20,27,43,30,np.nan,np.nan], 'Gender':['M',np.nan,'F','M','M','M']} df = pd.DataFrame(data) ... for index, value in df.dtypes.items(): if value == 'object': df[index] = df[index].fillna('') else: df[index] = df[index].fillna(0) print(df)
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ pandas
pandas: Replace NaN (missing values) with fillna() | note.nkmk.me
February 1, 2024 - In pandas, the fillna() method allows you to replace NaN values in a DataFrame or Series with a specific value.
Top answer
1 of 3
3

Randomly replace values in a numpy array

# The dataset
data = pd.read_csv('iris.data')
mat = data.iloc[:,:4].as_matrix()

Set the number of values to replace. For example 20%:

# Edit: changed len(mat) for mat.size
prop = int(mat.size * 0.2)

Randomly choose indices of the numpy array:

i = [random.choice(range(mat.shape[0])) for _ in range(prop)]
j = [random.choice(range(mat.shape[1])) for _ in range(prop)]

Change values with NaN

mat[i,j] = np.NaN

Dropout for any array dimension

Another way to do that with an array of more than 2 dimensions would be to use the numpy.put() function:

import numpy as np
import random
from sklearn import datasets

data = datasets.load_iris()['data']

def dropout(a, percent):
    # create a copy
    mat = a.copy()
    # number of values to replace
    prop = int(mat.size * percent)
    # indices to mask
    mask = random.sample(range(mat.size), prop)
    # replace with NaN
    np.put(mat, mask, [np.NaN]*len(mask))
    return mat

This function returns a modified array:

modified = dropout(data, 0.2)

We can verify that the correct number of values have been modified:

np.sum(np.isnan(modified))/float(data.size)

[out]:

0.2
2 of 3
1

Depending on the data structure you are keeping the values there might be different solutions.

If you are using Numpy arrays, you can employ np.insert method which is referred here:

import numpy as np
a = np.arrray([(122.0, 1.0, -47.0), (123.0, 1.0, -47.0), (125.0, 1.0, -44.0)]))
np.insert(a, 2, np.nan, axis=0)
array([[ 122.,    1.,  -47.],
       [ 123.,    1.,  -47.],
       [  nan,   nan,   nan],
       [ 125.,    1.,  -44.]])

If you are using Pandas you can use instance method replace on the objects of the DataFrames as referred here:

In [106]:
df.replace('N/A',np.NaN)

Out[106]:
    x    y
0  10   12
1  50   11
2  18  NaN
3  32   13
4  47   15
5  20  NaN

In the code above, the first argument can be your arbitrary input which you want to change.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ pandas .fillna() replacing every value with nan instead of replacing only nan values.
r/learnpython on Reddit: Pandas .fillna() replacing every value with NaN instead of replacing only NaN values.
October 10, 2021 -

Hello, I'm currently attempting the Kaggle housing prices challenge seen in this link. https://www.kaggle.com/c/house-prices-advanced-regression-techniques.

I have a concatenated table which combines the training and testing tables into one in order to handle all missing values at once.

combine_df = pd.concat([train, test], axis=0, sort=False)
combine_df.drop(['Id', 'SalePrice'], axis=1, inplace=True)

I then attempt to fill all NaN categorical values with the following lines below. Where null_columns is a list of columns that I want to replace NaN values.

combine_df[null_columns] = combine_df[null_columns].fillna('0', inplace=True)

However, this line changes every value in the columns into a NaN value instead of replacing NaN values with '0' as seen in the output below which shows the amount of NaN values for each column.

BsmtQual        2919
BsmtCond        2919
BsmtExposure    2919
BsmtFinType1    2919
BsmtFinType2    2919
GarageType      2919
GarageFinish    2919
GarageQual      2919
GarageCond      2919

I've tried using .replace, a lambda function, and also using .loc and all of them end up doing the same thing as the code above. What is going on with my code that causes this? I've also been unable to find anything regarding this on stack overflow. Any help would be greatly appreciated.

๐ŸŒ
TechOverflow
techoverflow.net โ€บ 2021 โ€บ 04 โ€บ 24 โ€บ how-to-replace-pandas-values-by-nan-by-threshold
How to replace pandas values by NaN by threshold | TechOverflow
December 24, 2025 - When processing pandas datasets, often you need to remove values above or below a given threshold from a dataset. One way to โ€œremoveโ€ values from a dataset is to replace them by NaN (not a number) values which are typically treated as โ€œmissingโ€ values. For example: In order to replace values of the xcolumn by NaNwhere the x column is< 0.75 in a DataFrame df, use this snippet:
๐ŸŒ
Statology
statology.org โ€บ home โ€บ pandas: how to replace nan values with string
Pandas: How to Replace NaN Values with String
November 1, 2021 - This tutorial explains how to replace NaN values in a pandas DataFrame with a specific string, including several examples.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas replace nan values with zero in a column
Pandas Replace NaN Values with Zero in a Column - Spark By {Examples}
June 26, 2025 - You can use the pandas.DataFrame.fillna() or pandas.DataFrame.replace() methods to replace all NaN or None values in an entire DataFrame with zeros (0).
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas replace blank values (empty) with nan
Pandas Replace Blank Values (Empty) with NaN - Spark By {Examples}
June 26, 2025 - In pandas, you can replace blank values (empty strings) with NaN using the replace() method. In this article, I will explain the replacing blank values or
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-replace-a-value-in-pandas-with-nan
How to Replace a Value in Pandas with NaN | Saturn Cloud Blog
August 25, 2023 - Replacing a value with NaN in pandas is a common task in data analysis and manipulation. In this article, we discussed three different methods for replacing a value with NaN in pandas: loc, replace, and where. These methods provide different ways to achieve the same result, depending on the use case.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ replace-none-with-nan-in-pandas
How to replace None with NaN in Pandas DataFrame | bobbyhadz
April 11, 2024 - You can use the pandas.DataFrame.fillna() method to replace None with NaN in a pandas DataFrame. The method takes a value argument that is used to fill the holes.