Categorical Data in Python
? Handling Categorical Data in Python
Categorical data refers to variables that contain labels or categories rather than numerical values. In Python, categorical data is commonly handled using pandas, NumPy, and Scikit-learn.
? Types of Categorical Data
1?? Nominal Data ? No order (e.g., Gender: Male, Female)
2?? Ordinal Data ? Ordered categories (e.g., Education: High School < Bachelor's < Master's)
? 1?? Handling Categorical Data Using Pandas
? Example Dataset
import pandas as pd# Create a sample datasetdata = {'Color': ['Red', 'Blue', 'Green', 'Red', 'Blue'], 'Size': ['Small', 'Large', 'Medium', 'Large', 'Small'], 'Price': [10, 15, 12, 20, 8]}df = pd.DataFrame(data)print(df)? Output:
Color Size Price0 Red Small 101 Blue Large 152 Green Medium 123 Red Large 204 Blue Small 8? 2?? Convert Categorical Data to Numerical
? Method 1: Label Encoding (Ordinal Encoding)
Used when the categories have an order.
from sklearn.preprocessing import LabelEncoderle = LabelEncoder()df['Size'] = le.fit_transform(df['Size']) # Encode Size columnprint(df)print(le.classes_) # Check label mapping? Output:
Color Size Price0 Red 2 101 Blue 0 152 Green 1 123 Red 0 204 Blue 2 8['Large' 'Medium' 'Small']?? Use this only for ordinal categories! Otherwise, it may create misleading relationships.
? Method 2: One-Hot Encoding (OHE)
Used when categories are nominal (no order). It creates dummy variables for each category.
df_encoded = pd.get_dummies(df, columns=['Color'], drop_first=True)print(df_encoded)? Output:
Size Price Color_Blue Color_Green Color_Red0 2 10 0 0 11 0 15 1 0 02 1 12 0 1 03 0 20 0 0 14 2 8 1 0 0? Avoids misleading numerical relationships and is commonly used for machine learning models.
? Method 3: Using Category dtype in Pandas
Pandas provides an efficient way to handle categorical data.
df['Color'] = df['Color'].astype('category')print(df.dtypes) # Shows 'category' dtype? Choosing the Right Method
| Method | Best For | Pros | Cons |
|---|---|---|---|
| Label Encoding | Ordered (ordinal) data | Simple, memory efficient | May introduce false relationships |
| One-Hot Encoding | Unordered (nominal) data | No false relationships, widely used | Increases feature space |
| Category dtype | Data storage & analysis | Memory-efficient, faster operations | Not directly used in ML models |
? Need help with a specific dataset? Let me know!