You can using category dtype in sklearn , it should be labelencoder

df.city=df.city.astype('category').cat.codes
df
Out[385]: 
   school  city  category  capacity
0       1     0        45        23
1       2     1        12       236
2       3     2         8        63
3       4     0         7       234
Answer from BENY on Stack Overflow
🌐
Practical Business Python
pbpython.com › categorical-encoding.html
Guide to Encoding Categorical Values in Python - Practical Business Python
Before we go into some of the more “standard” approaches for encoding categorical data, this data set highlights one potential approach I’m calling “find and replace.” · There are two columns of data where the values are words used to represent numbers. 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 .
🌐
CodeSignal
codesignal.com › learn › courses › cleaning-and-transforming-data-with-pandas › lessons › encoding-categorical-variables-using-python
Encoding Categorical Variables Using Python
In this lesson, we will specifically focus on using a dictionary mapping to encode a binary categorical variable, which is a form of label encoding. Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignalStart learning today!
🌐
Medium
medium.com › @jaberi.mohamedhabib › encoding-categorical-variables-methods-and-techniques-in-pandas-scikit-learn-and-using-dummy-216ae2d5128d
Encoding Categorical Variables: Methods and Techniques in Pandas, Scikit-learn, and Using Dummy Function | by JABERI Mohamed Habib | Medium
September 27, 2024 - Label Encoding is the simplest form of encoding categorical variables. It assigns a unique integer value to each category. This technique works well when the categories have an inherent ordinal relationship (e.g., Low, Medium, High), but it ...
🌐
Turing
turing.com › kb › convert-categorical-data-in-pandas-and-scikit-learn
How to Convert Categorical Data in Pandas and Scikit-learn
We generally use one-hot encoding to solve the disadvantage of label encoding. The strategy is to convert each category into a column and assign it a 1 or 0 value. It is a process of creating dummy variables.
🌐
Medium
medium.com › analytics-vidhya › categorical-encoding-with-pandas-get-dummies-d6f1ae6a3e06
Categorical Encoding with Pandas: get_dummies | by Samuel Kehinde Ayo | Analytics Vidhya | Medium
September 17, 2021 - For this, we will implement get_dummies. What get_dummies does is, it creates a one-hot encoded matrix for every target column we specify but what about label encoding and when do I use which for which?
🌐
DataCamp
datacamp.com › tutorial › categorical-data
Handling Machine Learning Categorical Data with Python Tutorial | DataCamp
February 23, 2023 - One hot encoding is a process of representing categorical data as a set of binary values, where each category is mapped to a unique binary value. In this representation, only one bit is set to 1, and the rest are set to 0, hence the name "one hot." This is commonly used in machine learning to convert categorical data into a format that algorithms can process. ... One way to achieve this in pandas is by using the `pd.get_dummies()` method.
🌐
Analytics Vidhya
analyticsvidhya.com › home › what are categorical data encoding methods | binary encoding
What are Categorical Data Encoding Methods | Binary Encoding
May 1, 2025 - In target encoding, we calculate the mean of the target variable for each category and replace the category variable with the mean value. In the case of the categorical target variables, the posterior probability of the target replaces each category.
Find elsewhere
🌐
Scikit-learn course
inria.github.io › scikit-learn-mooc › python_scripts › 03_categorical_pipeline.html
Encoding of categorical variables — Scikit-learn course
In this notebook, we present some typical ways of dealing with categorical variables by encoding them, namely ordinal encoding and one-hot encoding. Let’s first load the entire adult dataset containing both numerical and categorical data. import pandas as pd adult_census = pd.read_csv("....
🌐
KDnuggets
kdnuggets.com › 2023 › 07 › pandas-onehot-encode-data.html
Pandas: How to One-Hot Encode Data - KDnuggets
July 24, 2023 - df_encoded = pd.get_dummies(df, columns=['categorical_column', ]) The following commands drops the categorical_column and creates a new column for each unique value. Therefore, the single categorical column is converted into 4 new columns where only one of the 4 columns will have a 1 value, and all of the other 3 are encoded 0.
🌐
Kaggle
kaggle.com › code › paulrohan2020 › tutorial-encoding-categorical-variables
Tutorial-Encoding-Categorical-Variables | Kaggle
April 19, 2022 - Explore and run AI code with Kaggle Notebooks | Using data from Breast cancer data
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › encoding-categorical-data-in-sklearn
Encoding Categorical Data in Sklearn - GeeksforGeeks
September 17, 2025 - Now we will use One-Hot encoding which creates separate binary columns for each category, ideal for nominal data with no natural order.
🌐
Towards Data Science
towardsdatascience.com › home › data science › encoding categorical data, explained: a visual guide with code example for beginners
Encoding Categorical Data, Explained: A Visual Guide with Code Example for Beginners | Towards Data Science
September 2, 2024 - Let's use a simple golf dataset to illustrate our encoding methods (and it has mostly categorical columns). This dataset records various weather conditions and the resulting crowdedness at a golf course. ... import pandas as pdimport numpy as npdata = { 'Date': ['03-25', '03-26', '03-27', '03-28', '03-29', '03-30', '03-31', '04-01', '04-02', '04-03', '04-04', '04-05'], 'Weekday': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri'], 'Month': ['Mar', 'Mar', 'Mar', 'Mar', 'Mar', 'Mar', 'Mar', 'Apr', 'Apr', 'Apr', 'Apr', 'Apr'], 'Temperature': ['High', 'Low', 'High
🌐
Pandas
pandas.pydata.org › docs › user_guide › categorical.html
Categorical data — pandas 3.0.6 documentation - PyData |
Currently, categorical data and the underlying Categorical is implemented as a Python object and not as a low-level NumPy array dtype.
🌐
Hyperskill
hyperskill.org › learn › step › 32241
Preprocessing categorical features with pandas
Hyperskill is an educational platform for learning programming and software development through project-based courses, that helps you secure a job in tech. Master Python, Java, Kotlin, and more with real-world coding challenges.
🌐
CodeSignal
codesignal.com › learn › courses › data-transformation-techniques-in-pandas › lessons › handling-categorical-data
Handling Categorical Data | CodeSignal Learn
One-hot encoding creates new columns ... categorical types: For memory efficiency and better performance. How to perform the conversion: Using the astype('category') method in Pandas....
🌐
The Neural Base
theneuralbase.com › home › pytorch & ml › how to encode categorical variables in pandas
How to encode categorical variables in pandas for PyTorch models
Use pandas.Categorical or pandas.factorize for label encoding categorical variables, and pandas.get_dummies for one-hot encoding. These methods prepare categorical data for PyTorch models by converting categories into numeric formats.
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › 3 ways to encode categorical variables for deep learning
3 Ways to Encode Categorical Variables for Deep Learning - MachineLearningMastery.com
August 26, 2020 - A reasonable classification accuracy score on this dataset is between 68% and 73%. We will aim for this region, but note that the models in this tutorial are not optimized: they are designed to demonstrate encoding schemes. You can download the dataset and save the file as “breast-cancer.csv” in your current working directory. ... Looking at the data, we can see that all nine input variables are categorical. Specifically, all variables are quoted strings; some are ordinal and some are not. We can load this dataset into memory using the Pandas library.
🌐
Medium
soumenatta.medium.com › categorical-data-encoding-techniques-in-python-a-complete-guide-a913aae19a22
Categorical Data Encoding Techniques in Python: A Complete Guide | by Dr. Soumen Atta, Ph.D. | Medium
May 4, 2023 - In this tutorial, we will explore various techniques for categorical data encoding in Python. We will be using the scikit-learn library for our examples. Scikit-learn is a popular library for machine learning in Python. Let’s start by importing the necessary libraries: import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.preprocessing import OneHotEncoder from sklearn.feature_extraction.text import CountVectorizer
🌐
Kaggle
kaggle.com › code › ohseokkim › preprocessing-encoding-categorical-data
[Preprocessing] Encoding Categorical Data
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds