How to convert categorical variables into numerical variables in Python?
DATA MUNGING

How to convert categorical variables into numerical variables in Python?

How to convert categorical variables into numerical variables in Python?

This recipe helps you convert categorical variables into numerical variables in Python

0
This python source code does the following: 1. Creates dictionary and converts it into dataframe 2. Uses "get_dummies" function for the encoding 3. Concats the final encoded dataset into the final dataframe 4. Drops categorical variable column
In [1]:
## How to convert categorical variables into numerical variables in Python
def Kickstarter_Example_76():
    print()
    print(format('How to convert categorical variables into numerical variables in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd

    # Create a dataframe
    data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
                'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
                'gender': ['male', 'female', 'male', 'female', 'female']}

    df = pd.DataFrame(data, columns = ['first_name', 'last_name', 'gender'])
    print(); print(df)

    # Create a set of dummy variables from the gender variable
    df_gender = pd.get_dummies(df['gender'])

    # Join the dummy variables to the main dataframe
    df_new = pd.concat([df, df_gender], axis=1)
    print(); print(df_new)

    # Alterative for joining the new columns
    df_new = df.join(df_gender)
    print(); print(df_new)

Kickstarter_Example_76()
*****How to convert categorical variables into numerical variables in Python******

  first_name last_name  gender
0      Jason    Miller    male
1      Molly  Jacobson  female
2       Tina       Ali    male
3       Jake    Milner  female
4        Amy     Cooze  female

  first_name last_name  gender  female  male
0      Jason    Miller    male       0     1
1      Molly  Jacobson  female       1     0
2       Tina       Ali    male       0     1
3       Jake    Milner  female       1     0
4        Amy     Cooze  female       1     0

  first_name last_name  gender  female  male
0      Jason    Miller    male       0     1
1      Molly  Jacobson  female       1     0
2       Tina       Ali    male       0     1
3       Jake    Milner  female       1     0
4        Amy     Cooze  female       1     0

Relevant Projects

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Data Science Project-All State Insurance Claims Severity Prediction
Data science project in R to develop automated methods for predicting the cost and severity of insurance claims.