What about DataFrame.replace?

In [9]: mapping = {'set': 1, 'test': 2}

In [10]: df.replace({'set': mapping, 'tesst': mapping})
Out[10]: 
   Unnamed: 0 respondent  brand engine  country  aware  aware_2  aware_3  age  \
0           0          a  volvo      p      swe      1        0        1   23   
1           1          b  volvo   None      swe      0        0        1   45   
2           2          c    bmw      p       us      0        0        1   56   
3           3          d    bmw      p       us      0        1        1   43   
4           4          e    bmw      d  germany      1        0        1   34   
5           5          f   audi      d  germany      1        0        1   59   
6           6          g  volvo      d      swe      1        0        0   65   
7           7          h   audi      d      swe      1        0        0   78   
8           8          i  volvo      d       us      1        1        1   32   

  tesst set  
0     2   1  
1     1   2  
2     2   1  
3     1   2  
4     2   1  
5     1   2  
6     2   1  
7     1   2  
8     2   1  

As @Jeff pointed out in the comments, in pandas versions < 0.11.1, manually tack .convert_objects() onto the end to properly convert tesst and set to int64 columns, in case that matters in subsequent operations.

Answer from Dan Allan on Stack Overflow
🌐
Saturn Cloud
saturncloud.io › blog › how-to-replace-strings-with-numbers-in-python-pandas-dataframe
How to Replace Strings with Numbers in Python Pandas Dataframe | Saturn Cloud Blog
May 1, 2026 - The replace() method is a convenient way to replace specific strings in a dataframe column with numbers. The syntax for using the replace() method is as follows: df['column_name'].replace({'string_to_replace': 'replacement_value'}, inplace=True) ...
Discussions

python - replace strings in every column with numbers - Stack Overflow
To convert a column with String entries, one should do a map and then pandas.replace(). ... As seen above, the last two column's strings are replaced with numbers representing these strings. More on stackoverflow.com
🌐 stackoverflow.com
May 18, 2021
python pandas - replace number with string - Stack Overflow
Read more: http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.Series.map.html ... Sign up to request clarification or add additional context in comments. ... You need to convert your column 'A' as type string. More on stackoverflow.com
🌐 stackoverflow.com
python - Replace string values in pandas rows with numbers with the help of for loop - Stack Overflow
I have 10 unique values in one column from my dataframe. For example below is the dataframe df['categories'].unique() output is : Electronic Computers Mobile Phone Router Food I want to replace ' More on stackoverflow.com
🌐 stackoverflow.com
Replacing an int value in a cell with a string value in pandas
DataFrames are strongly typed; if a column is of the int type, then it cannot contain strings. More on reddit.com
🌐 r/learnpython
4
1
February 24, 2022
🌐
IncludeHelp
includehelp.com › python › pandas-replacing-strings-in-dataframe-with-numbers.aspx
Python - Pandas replacing strings in dataframe with numbers
Suppose, we have a DataFrame that ... is not possible. In this case, we will replace all these strings with numerical values using pandas.to_numeric() method....
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 3.0.6 documentation
Simple string replacement. ... Regex substitution is performed under the hood with re.sub. The rules for substitution for re.sub are the same. Regular expressions will only substitute on strings, meaning you cannot provide, for example, a regular expression matching floating point numbers and expect the columns in your frame that have a numeric dtype to be matched.
🌐
TutorialsPoint
tutorialspoint.com › replacing-strings-with-numbers-in-python-for-data-analysis
Replacing strings with numbers in Python for Data Analysis
Here is a way to replace our string(column values) to integers. #Import required library import pandas as pd #Import the CSV file into Python using read_csv() from pandas dataframe = pd.read_csv("data_pandas1.csv") #Create the dictionary of key-value pair, where key is #your old value(string) and value is your new value(integer).
🌐
GeeksforGeeks
geeksforgeeks.org › replacing-strings-with-numbers-in-python-for-data-analysis
Replacing strings with numbers in Python for Data Analysis | GeeksforGeeks
February 5, 2018 - To create a dictionary containing two elements with following key-value pair: Key Value male 1 female 2 Then iterate using for loop through Gender column of DataFrame and replace the values wherever the keys are found. ... # import pandas library import pandas as pd # creating file handler for # our example.csv file in # read mode file_handler = open("example.csv", "r") # creating a Pandas DataFrame # using read_csv function # that reads from a csv file.
🌐
Stack Overflow
stackoverflow.com › questions › 67588148 › replace-strings-in-every-column-with-numbers
python - replace strings in every column with numbers - Stack Overflow
May 18, 2021 - import pandas as pd def convertStringColumnsToNum(data): columns = data.columns columns_dtypes = data.dtypes maps = [] for col_idx in range(0, len(columns)): # don't change columns already comprising of numbers if(columns_dtypes[col_idx] == 'int64'): # can be extended to more dtypes continue # inspired from Shivam Roy's answer col = columns[col_idx] tmp = pd.Categorical(data[col]) data[col] = tmp.codes maps.append(tmp.categories) return maps
Find elsewhere
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas 3.0.6 documentation
Dictionary contains <key : value> pairs of strings to be replaced along with the updated value. ... Replacement string or a callable. The callable is passed the regex match object and must return a replacement string to be used. Must have a value of None if pat is a dict See re.sub(). ... Number of ...
🌐
Martinhjelm
martinhjelm.github.io › 2017 › 11 › 12 › Pandas-Replacing-Strings-In-A-Column
Pandas - How to replace string values in a column with integer numbers · My 404s
November 12, 2017 - Lets assume that a column named labels contains the label strings [A,B,C] and we want to replace them with [0,1,2]. Then we can do the following,
🌐
DNMTechs
dnmtechs.com › replacing-strings-with-numbers-in-python-pandas-dataframe
Replacing Strings with Numbers in Python Pandas Dataframe – DNMTechs – Sharing and Storing Technology Knowledge
However, it requires manually defining the mapping for each unique value, which can be cumbersome for larger datasets. Pandas provides a built-in function called factorize() that automatically assigns unique numerical values to each distinct string value in a column.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 3.0.5 documentation
Simple string replacement. ... Regex substitution is performed under the hood with re.sub. The rules for substitution for re.sub are the same. Regular expressions will only substitute on strings, meaning you cannot provide, for example, a regular expression matching floating point numbers and expect the columns in your frame that have a numeric dtype to be matched.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › how to replace string in pandas dataframe
How to Replace String in Pandas DataFrame - Spark By {Examples}
June 13, 2025 - In pandas, to replace a string in the DataFrame column, you can use either the replace() function or the str.replace() method along with lambda methods.
🌐
Codersarts
codersarts.com › post › replacing-strings-with-numbers-in-python-for-data-analysis-with-pandas
Replacing strings with numbers in Python for Data Analysis with pandas
March 23, 2021 - Sometimes data is given in string format which is not fit into ML models, to solve this issue first changing a string value into any numeric value and then split it into training and testing data. Let we will learn below data and fit it into ML models then we need to change sex column value into numeric like-
🌐
GeeksforGeeks
geeksforgeeks.org › data analysis › python-pandas-dataframe-replace
Python | Pandas dataframe.replace() - GeeksforGeeks
Pandas dataframe.replace() function is used to replace a string, regex, list, dictionary, series, number, etc. from a Pandas Dataframe in Python.
Published: July 11, 2025
🌐
datagy
datagy.io › home › pandas tutorials › pandas dataframes › pandas replace() – replace values in pandas dataframe
Pandas replace() - Replace Values in Pandas Dataframe • datagy
March 2, 2023 - In this post, you’ll learn how to use the Pandas .replace() method to replace data in your DataFrame. The Pandas DataFrame.replace() method can be used to replace a string, values, and even regular expressions (regex) in your DataFrame.
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Replace values in DataFrame and Series with replace() | note.nkmk.me
January 17, 2024 - In pandas, the replace() method allows you to replace values in DataFrame and Series. It is also possible to replace parts of strings using regular expressions (regex). pandas.DataFrame.replace — pan ...
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › python-pandas-series-str-replace-to-replace-text-in-a-series
Python | Pandas Series.str.replace() to replace text in a series - GeeksforGeeks
July 11, 2025 - # overwriting column with replaced value of age data["Age"]= data["Age"].replace(25.0, "Twenty five") # creating a filter for age column # where age = "Twenty five" filter = data["Age"]=="Twenty five" # printing only filtered columns data.where(filter).dropna() Output: As shown in the output, all the values in Age column having age=25.0 have been replaced by "Twenty five". Name Team Number Position Age Height Weight College Salary 0 Avery Bradley Boston Celtics 0.0 PG Twenty five 6-2 180.0 Texas 7730337.0 1 Jae Crowder Boston Celtics 99.0 SF Twenty five 6-6 235.0 Marquette 6796117.0 7 Kelly Ol