Editor

Map Values with a Dictionary

Intermediate · Write the code · from Pandas apply(), map(), transform(): Custom Data Transformations

From the lesson: Pandas apply(), map(), transform(): Custom Data Transformations

Pandas gives you dozens of built-in methods like .sum(), .mean(), and .str.upper(). But what happens when you need to run your own custom logic on every row, column, or cell? That is where apply(), map(), and transform() come in.

Read the lesson

Use Series.map() with a dictionary to convert letter grades to GPA points. Map: A=4.0, B=3.0, C=2.0, D=1.0, F=0.0.

Print the resulting GPA Series as a list.

import pandas as pd
grades = pd.Series(['A', 'B', 'C', 'A', 'F', 'B'])
# Map grades to GPA points and print as a list

More Pandas Apply and Transform exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz