How to map values in a Pandas DataFrame?
0

How to map values in a Pandas DataFrame?

This recipe helps you map values in a Pandas DataFrame

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

Big Data Project 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.
Big Data Project Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.
Big Data Project Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.
Big Data Project Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.
Big Data Project 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.
Big Data Project 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.
Big Data Project 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.
Big Data Project Anomaly Detection Using Deep Learning and Autoencoders
Deep Learning Project- Learn about implementation of a machine learning algorithm using autoencoders for anomaly detection.
Big Data Project Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.
Big Data Project Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.