Predict the map() Output
Intermediate · Predict the output · 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 lessonWhat will this code print?
import pandas as pd
s = pd.Series([1, 2, 3, 4])
print(s.map(lambda x: x ** 2 if x % 2 == 0 else x).tolist())# Type the exact output
More Pandas Apply and Transform exercises
- Map Values with a DictionaryIntermediate
- Row-Level ApplyAdvanced
- Element-wise DataFrame FormattingAdvanced
- Refactor apply() to VectorizedAdvanced
- Fix the apply() BugAdvanced
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz