Challenge - 5 Problems
Map Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of map() with dictionary mapping
What is the output of the following code snippet?
Pandas
import pandas as pd s = pd.Series(['cat', 'dog', 'bird', 'cat']) mapping = {'cat': 'feline', 'dog': 'canine'} result = s.map(mapping) print(result)
Attempts:
2 left
💡 Hint
Think about what happens when a value is not found in the mapping dictionary.
✗ Incorrect
The map() function replaces values found in the dictionary. Values not found become NaN (missing). Here, 'bird' is not in the dictionary, so it becomes NaN.
❓ data_output
intermediate2:00remaining
Result of map() with a function
Given the code below, what is the resulting Series after applying map()?
Pandas
import pandas as pd s = pd.Series([1, 2, 3, 4]) result = s.map(lambda x: x**2) print(result)
Attempts:
2 left
💡 Hint
The lambda function squares each element.
✗ Incorrect
map() applies the lambda function to each element, squaring the numbers: 1²=1, 2²=4, 3²=9, 4²=16.
🔧 Debug
advanced2:00remaining
Identify the error in map() usage
What error does the following code raise?
Pandas
import pandas as pd s = pd.Series(['apple', 'banana', 'cherry']) result = s.map(['fruit', 'yellow', 'red']) print(result)
Attempts:
2 left
💡 Hint
Check what types map() accepts as its argument.
✗ Incorrect
map() expects a function, dictionary, or Series. Passing a list causes a TypeError.
🚀 Application
advanced2:30remaining
Using map() to replace values with a default
You want to replace values in a Series using a dictionary but keep original values if not found in the dictionary. Which code achieves this?
Pandas
import pandas as pd s = pd.Series(['red', 'blue', 'green', 'yellow']) mapping = {'red': '#FF0000', 'blue': '#0000FF'}
Attempts:
2 left
💡 Hint
Think about how to keep original values when mapping returns NaN.
✗ Incorrect
map() replaces known values and returns NaN for unknown. fillna(s) replaces NaN with original values.
🧠 Conceptual
expert3:00remaining
Understanding map() behavior with mixed types
Consider a Series with mixed types. What will be the output of map() when mapping integers to strings but the Series contains strings and integers?
Pandas
import pandas as pd s = pd.Series([1, '2', 3, '4']) mapping = {1: 'one', 2: 'two', 3: 'three', 4: 'four'} result = s.map(mapping) print(result)
Attempts:
2 left
💡 Hint
map() matches keys exactly, including type.
✗ Incorrect
The keys in mapping are integers. The Series has strings '2' and '4' which do not match integer keys, so those map to NaN.