0
0
Pandasdata~20 mins

applymap() for DataFrame-wide operations in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
applymap() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
   A  B
0  2  6
1  4  8
B
   A  B
0  1  3
1  2  4
C
   A  B
0  1  6
1  4  8
D
   A  B
0  2  3
1  4  8
Attempts:
2 left
💡 Hint
applymap applies the function to every element individually.
data_output
intermediate
2: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)
A
   X  Y
0  0  0
1  4  6
B
   X  Y
0  1  3
1  4  6
C
   X  Y
0  0  3
1  4  0
D
   X  Y
0  1  0
1  0  6
Attempts:
2 left
💡 Hint
Check which numbers are odd and replace them with zero.
🔧 Debug
advanced
2: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)
AValueError: Must produce scalar value
BNo error, prints DataFrame with lists as elements
CAttributeError: 'list' object has no attribute 'applymap'
DTypeError: unhashable type: 'list'
Attempts:
2 left
💡 Hint
applymap applies the function to each element and can return any object.
🚀 Application
advanced
2: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)
Alambda x: str(round(x, 2))
Blambda x: f"{x:.2f}"
Clambda x: format(x, '.2f') if isinstance(x, float) else x
Dlambda x: round(x, 2)
Attempts:
2 left
💡 Hint
Only floats should be formatted as strings, other types remain unchanged.
🧠 Conceptual
expert
2:00remaining
Understanding applymap() vs apply() on DataFrames
Which statement correctly describes the difference between applymap() and apply() when used on a pandas DataFrame?
Aapplymap() applies a function along rows; apply() applies a function element-wise to each cell.
Bapplymap() and apply() are interchangeable and produce the same results.
Capplymap() works only on Series; apply() works only on DataFrames.
Dapplymap() applies a function element-wise to each cell; apply() applies a function along rows or columns.
Attempts:
2 left
💡 Hint
Think about whether the function is applied to each cell or to rows/columns.