Challenge - 5 Problems
applymap() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of applymap() with a simple function
What is the output of this code when applying
applymap() to double each element in the DataFrame?Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) result = df.applymap(lambda x: x * 2) print(result)
Attempts:
2 left
💡 Hint
applymap applies the function to every element individually.
✗ Incorrect
The lambda function doubles each element. So 1 becomes 2, 2 becomes 4, 3 becomes 6, and 4 becomes 8.
❓ data_output
intermediate2:00remaining
Result of applymap() with a conditional function
Given this DataFrame, what is the result of applying
applymap() with a function that replaces odd numbers with 0 and keeps even numbers unchanged?Pandas
import pandas as pd df = pd.DataFrame({'X': [1, 4], 'Y': [3, 6]}) result = df.applymap(lambda x: 0 if x % 2 != 0 else x) print(result)
Attempts:
2 left
💡 Hint
Check which numbers are odd and replace them with zero.
✗ Incorrect
1 and 3 are odd, so they become 0. 4 and 6 are even, so they stay the same.
🔧 Debug
advanced2:00remaining
Identify the error in applymap() usage
What error will this code raise when trying to use
applymap() with a function that returns a list?Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) result = df.applymap(lambda x: [x, x*2]) print(result)
Attempts:
2 left
💡 Hint
applymap applies the function to each element and can return any object.
✗ Incorrect
applymap applies the function to each element and stores the returned object. Returning a list is allowed, so no error occurs.
🚀 Application
advanced2:00remaining
Using applymap() to format DataFrame values
You want to format all float numbers in a DataFrame to show only two decimal places as strings. Which applymap() function achieves this?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1.2345, 2.3456], 'B': [3.4567, 4.5678]}) result = df.applymap(??? ) print(result)
Attempts:
2 left
💡 Hint
Only floats should be formatted as strings, other types remain unchanged.
✗ Incorrect
Option C formats only floats to strings with two decimals, leaving other types untouched.
🧠 Conceptual
expert2:00remaining
Understanding applymap() vs apply() on DataFrames
Which statement correctly describes the difference between
applymap() and apply() when used on a pandas DataFrame?Attempts:
2 left
💡 Hint
Think about whether the function is applied to each cell or to rows/columns.
✗ Incorrect
applymap() works element-wise on each cell of a DataFrame. apply() works on rows or columns (Series) and can aggregate or transform.