0
0
Pandasdata~20 mins

map() for element-wise transformation in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Map Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
0    feline
1    canine
2    None
3    feline
dtype: object
B
0    cat
1    dog
2    bird
3    cat
dtype: object
C
0    feline
1    canine
2    bird
3    feline
dtype: object
D
0    feline
1    canine
2      NaN
3    feline
dtype: object
Attempts:
2 left
💡 Hint
Think about what happens when a value is not found in the mapping dictionary.
data_output
intermediate
2: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)
A
0     1
1     4
2     9
3    16
dtype: int64
B
0     2
1     3
2     4
3     5
dtype: int64
C
0     1
1     8
2    27
3    64
dtype: int64
D
0     1
1     2
2     3
3     4
dtype: int64
Attempts:
2 left
💡 Hint
The lambda function squares each element.
🔧 Debug
advanced
2: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)
ATypeError: mapping must be a callable, dict, or Series
BTypeError: unhashable type: 'list'
CValueError: Mapping correspondence is not valid
DNo error, outputs a Series with mapped values
Attempts:
2 left
💡 Hint
Check what types map() accepts as its argument.
🚀 Application
advanced
2: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'}
As.map(mapping).fillna('unknown')
Bs.map(lambda x: mapping.get(x))
Cs.map(mapping).fillna(s)
Ds.map(mapping).replace({None: s})
Attempts:
2 left
💡 Hint
Think about how to keep original values when mapping returns NaN.
🧠 Conceptual
expert
3: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)
A
0      one
1      two
2    three
3      four
dtype: object
B
0      one
1      NaN
2    three
3      NaN
dtype: object
C
0      NaN
1      NaN
2      NaN
3      NaN
dtype: object
D
0      one
1      2
2    three
3      4
dtype: object
Attempts:
2 left
💡 Hint
map() matches keys exactly, including type.