How to map values in a Pandas DataFrame?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to map values in a Pandas DataFrame?

How to map values in a Pandas DataFrame?

This recipe helps you map values in a Pandas DataFrame

0
This data science python source code does the following: 1. Imports the necessary libraries. 2. Creates data dictionary and converts it to pandas dataframe. 3. Utilizes map function for allocating values to a column.
In [1]:
## How to map values in a Pandas DataFrame
def Kickstarter_Example_93():
    print()
    print(format('How to map values in a Pandas DataFrame','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd

    # Create dataframe
    raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
                'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
                'age': [42, 52, 36, 24, 73],
                'city': ['San Francisco', 'Baltimore', 'Miami', 'Douglas', 'Boston']}
    df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'city'])
    print(); print(df)

    # Create a dictionary of values
    city_to_state = {'San Francisco' : 'California',
                     'Baltimore' : 'Maryland',
                     'Miami' : 'Florida',
                     'Douglas' : 'Arizona',
                     'Boston' : 'Massachusetts'}
    print(); print(city_to_state)

    # Map the values of the city_to_state dictionary to the values in the city variable
    df['state'] = df['city'].map(city_to_state)
    print(); print(df)

Kickstarter_Example_93()
*********************How to map values in a Pandas DataFrame**********************

  first_name last_name  age           city
0      Jason    Miller   42  San Francisco
1      Molly  Jacobson   52      Baltimore
2       Tina       Ali   36          Miami
3       Jake    Milner   24        Douglas
4        Amy     Cooze   73         Boston

{'San Francisco': 'California', 'Baltimore': 'Maryland', 'Miami': 'Florida', 'Douglas': 'Arizona', 'Boston': 'Massachusetts'}

  first_name last_name  age           city          state
0      Jason    Miller   42  San Francisco     California
1      Molly  Jacobson   52      Baltimore       Maryland
2       Tina       Ali   36          Miami        Florida
3       Jake    Milner   24        Douglas        Arizona
4        Amy     Cooze   73         Boston  Massachusetts

Relevant Projects

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

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.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

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 Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.