First, to convert a Categorical column to its numerical codes, you can do this easier with: dataframe['c'].cat.codes.
Further, it is possible to select automatically all columns with a certain dtype in a dataframe using select_dtypes. This way, you can apply above operation on multiple and automatically selected columns.

First making an example dataframe:

CopyIn [75]: df = pd.DataFrame({'col1':[1,2,3,4,5], 'col2':list('abcab'),  'col3':list('ababb')})

In [76]: df['col2'] = df['col2'].astype('category')

In [77]: df['col3'] = df['col3'].astype('category')

In [78]: df.dtypes
Out[78]:
col1       int64
col2    category
col3    category
dtype: object

Then by using select_dtypes to select the columns, and then applying .cat.codes on each of these columns, you can get the following result:

CopyIn [80]: cat_columns = df.select_dtypes(['category']).columns

In [81]: cat_columns
Out[81]: Index([u'col2', u'col3'], dtype='object')

In [83]: df[cat_columns] = df[cat_columns].apply(lambda x: x.cat.codes)

In [84]: df
Out[84]:
   col1  col2  col3
0     1     0     0
1     2     1     1
2     3     2     0
3     4     0     1
4     5     1     1

Note:

  1. NaN becomes -1
  2. This method is fast because the relationship between code and category is readily available and do not need to be computed.
Answer from joris on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-categorical-variable-to-numeric-in-pandas
How to Convert Categorical Variable to Numeric in Pandas? - GeeksforGeeks
July 23, 2025 - This is an ordinal type of categorical variable. We will convert their education levels into numeric terms. ... import pandas as pd # read csv file df = pd.read_csv('data.csv') # replacing values df['Education'].replace(['Under-Graduate', 'Diploma '], [0, 1], inplace=True)
Discussions

How to Transform Categorical Data to Numerical Data Using Pandas
Pd.get_dummies(data) More on reddit.com
🌐 r/learnmachinelearning
6
8
November 29, 2021
In Pandas, how do I transform a categorical column with a related numeric column into several numeric columns with the categories as headers?
df.pivot(columns='SENTIMENT', values='CONFIDENCE') More on reddit.com
🌐 r/learnpython
9
2
July 7, 2022
🌐
Statology
statology.org › home › how to convert categorical variable to numeric in pandas
How to Convert Categorical Variable to Numeric in Pandas
October 28, 2021 - This tutorial explains how to convert a categorical variable to a numeric variable in a pandas DataFrame, including an example.
🌐
Reddit
reddit.com › r/learnmachinelearning › how to transform categorical data to numerical data using pandas
r/learnmachinelearning on Reddit: How to Transform Categorical Data to Numerical Data Using Pandas
November 29, 2021 -

I am writing a python program that uses logistic regression to predict an outcome based on survey data from a csv. However, I'm running into the issue that some survey data is non-numerical. I need to:

  • transform categorical data to numerical data, without knowing which columns are categorical or how many categories per column there are ahead of time

  • be able to map the numerical data onto the category labels later

Any suggestions on how to approach this? I sincerely appreciate any thoughts!

Example data:

weight systolic blood pressure has diabetes?
155 119 no
210 131 yes
301 143 yes

Example output:

weight systolic blood pressure has diabetes?
155 119 0
210 131 1
301 143 1
diabetes_dict = {
    0: "no",
    1: "yes"
}
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-categorical-data-to-numerical-data-with-pandas
How to Convert Categorical Data to Numerical Data with Pandas | Saturn Cloud Blog
June 19, 2023 - This attribute is available for categorical data types in Pandas and returns a numerical representation of each category. Here is an example of how to use the cat.codes attribute to convert categorical data to numerical data:
🌐
Turing
turing.com › kb › convert-categorical-data-in-pandas-and-scikit-learn
How to Convert Categorical Data in Pandas and Scikit-learn
Learn to convert categorical data into numerical data with Pandas and Scikit-learn using methods like find and replace, label encoding, and one-hot encoding.
Find elsewhere
🌐
Medium
medium.com › @rafiemon71 › techniques-for-converting-categorical-data-into-numerical-data-f1c9d0a3863f
Techniques for Converting Categorical Data into Numerical Data | by SnapWise | Medium
February 11, 2023 - This is the simplest way to convert categorical data into numerical data, and it can work well for small datasets or when the categories have a natural ordinal relationship. import pandas as pd # Create a sample dataframe df = pd.DataFrame({'col': ['A', 'B', 'C', 'A', 'B']}) # Use the Pandas factorize method to map the categorical values to integers df['col_numeric'] = pd.factorize(df['col'])[0] # Show the result print(df)
🌐
Arab Psychology
scales.arabpsychology.com › home › how to convert categorical data to numeric using pandas
How To Convert Categorical Data To Numeric Using Pandas
December 3, 2025 - One of the most straightforward and effective ways to convert a categorical column into numerical representations in Pandas is by utilizing the built-in function pd.factorize(). This function is designed to find unique values in a sequence and ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-categorical-string-data-into-numeric-in-python
How to convert categorical string data into numeric in Python? - GeeksforGeeks
July 23, 2025 - There are many ways to convert categorical data into numerical data. Here in this article, we’ll be discussing the two most used methods namely : ... We will be using pandas.get_dummies function to convert the categorical string data into numeric.
🌐
Medium
medium.com › @techwithpraisejames › how-to-turn-categorical-variables-into-numbers-using-python-pandas-get-dummies-method-5a7d0ae0b3a3
How to Convert Categorical Variables To Numbers Using Python Pandas get_dummies() Method | by Praise James | Medium
September 11, 2025 - How to Convert Categorical Variables To Numbers Using Python Pandas get_dummies() Method Learn how to change boolean value results to 0s and 1s when using the get_dummies method. Machine learning …
🌐
Fastml
fastml.com › converting-categorical-data-into-numbers-with-pandas-and-scikit-learn
Converting categorical data into numbers with Pandas and Scikit-learn - FastML
cols_to_transform = [ 'a', 'list', 'of', 'categorical', 'column', 'names' ] df_with_dummies = pd.get_dummies( columns = cols_to_transform ) This is the way we recommend now. (end update) We’ll use Pandas to load the data, do some cleaning and send it to Scikit-learn’s DictVectorizer.
🌐
ProjectPro
projectpro.io › recipes › convert-categorical-variables-into-numerical-variables-in-python
How to convert categorical variables into numerical in Python? -
September 6, 2023 - For converting categorical data to numerical data, the Python source code in this recipe does the following- 1. Creates dictionary and converts it into a dataframe ... The following steps will show you how to transform categorical variables in Python. ... We have only imported pandas this is reqired for dataset.
🌐
CodeSignal
codesignal.com › learn › courses › data-transformation-techniques-in-pandas › lessons › handling-categorical-data
Handling Categorical Data | CodeSignal Learn
How to perform the conversion: Using the astype('category') method in Pandas. Encoding examples: Label encoding and one-hot encoding to convert categories into numeric forms.
🌐
Kaggle
kaggle.com › questions-and-answers › 374226
Categorical to Numeric | Kaggle
How do i convert some categorical features to numeric ones? Suggest me an efficient technique of doing that. How to know which are all the categories need co...
🌐
Practical Business Python
pbpython.com › categorical-encoding.html
Guide to Encoding Categorical Values in Python - Practical Business Python
Specifically the number of cylinders in the engine and number of doors on the car. Pandas makes it easy for us to directly replace the text values with their numeric equivalent by using replace .
🌐
Benalexkeen
benalexkeen.com › mapping-categorical-data-in-pandas
Mapping Categorical Data in pandas – Ben Alex Keen
If we have our data in Series or Data Frames, we can convert these categories to numbers using pandas Series’ astype method and specify ‘categorical’. Nominal categories are unordered e.g. colours, sex, nationality. In the example below we categorise the Series vertebrates of the df dataframe into ...
🌐
DataScience Made Simple
datasciencemadesimple.com › home › convert column to categorical in pandas python
Convert column to categorical in pandas python - DataScience Made Simple
January 29, 2023 - Categorical function is used to convert / typecast integer or character column to categorical in pandas python. Typecast a numeric column to categorical using categorical function().
🌐
Replit
replit.com › home › discover › how to convert categorical data to numerical data in python
How to convert categorical data to numerical data in Python | Replit
February 20, 2026 - This method is more robust than manual mapping and integrates seamlessly into scikit-learn pipelines. When basic methods fall short, these advanced techniques can help you handle high-cardinality features and extract more predictive information from your categorical data. import category_encoders as ce import pandas as pd X = pd.DataFrame({'category': ['A', 'B', 'A', 'C', 'B']}) y = pd.Series([1, 0, 1, 1, 0]) # Target variable encoder = ce.TargetEncoder() encoded = encoder.fit_transform(X, y) print(encoded)--OUTPUT--category 0 1.000000 1 0.000000 2 1.000000 3 1.000000 4 0.000000