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

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

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

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.

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.

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.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.