applymap() for DataFrame-wide operations in Pandas - Time & Space Complexity
We want to understand how the time to run applymap() changes as the DataFrame gets bigger.
How does the number of cells affect the work done?
Analyze the time complexity of the following code snippet.
import pandas as pd
import numpy as np
n, m = 10, 10 # example sizes
df = pd.DataFrame(np.random.randint(0, 100, size=(n, m)))
result = df.applymap(lambda x: x * 2)
This code doubles every number in the DataFrame using applymap().
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying the function to each cell in the DataFrame.
- How many times: Once for every cell, so total cells = rows x columns.
As the DataFrame grows, the work grows with the number of cells.
| Input Size (rows x columns) | Approx. Operations |
|---|---|
| 10 x 10 = 100 | 100 function calls |
| 100 x 100 = 10,000 | 10,000 function calls |
| 1000 x 1000 = 1,000,000 | 1,000,000 function calls |
Pattern observation: The number of operations grows directly with the total number of cells.
Time Complexity: O(n x m)
This means the time grows proportionally to the total number of cells in the DataFrame.
[X] Wrong: "applymap() runs in constant time regardless of DataFrame size."
[OK] Correct: The function runs once per cell, so more cells mean more work and more time.
Knowing how applymap() scales helps you explain performance when working with large tables.
What if we changed applymap() to apply() on columns? How would the time complexity change?
Practice
applymap() function do in a pandas DataFrame?Solution
Step 1: Understand the purpose of applymap()
applymap()is designed to apply a function to every individual cell in a DataFrame, not just rows or columns.Step 2: Compare with other pandas functions
Functions likeapply()work on rows or columns, butapplymap()works cell-by-cell.Final Answer:
Applies a function to every single cell in the DataFrame. -> Option BQuick Check:
applymap() = cell-wise function application [OK]
- Confusing applymap() with apply()
- Thinking applymap() works on Series
- Assuming applymap() only works on numeric data
applymap() on a DataFrame df with a function func?Solution
Step 1: Recall the method call format
In pandas,applymap()is a method of DataFrame objects and is called asdf.applymap(func).Step 2: Check other options for syntax errors
Options B, C, and D do not follow valid Python or pandas syntax for method calls.Final Answer:
df.applymap(func) -> Option AQuick Check:
Method call syntax = df.applymap(func) [OK]
- Using assignment instead of method call
- Using square brackets instead of parentheses
- Calling applymap as a standalone function
df below:A B 0 1 4 1 2 5 2 3 6
What is the output of
df.applymap(lambda x: x * 2)?Solution
Step 1: Understand the lambda function
The lambda function doubles each value:lambda x: x * 2.Step 2: Apply the function to each cell
Each cell in the DataFrame is multiplied by 2, so 1 becomes 2, 4 becomes 8, etc.Final Answer:
A B 0 2 8 1 4 10 2 6 12 -> Option CQuick Check:
Each cell doubled = output B [OK]
- Thinking applymap works column-wise
- Expecting original DataFrame unchanged
- Assuming lambda functions cause errors
import pandas as pd
df = pd.DataFrame({'A':[1,2], 'B':[3,4]})
result = df.applymap(lambda x: x + 1)Why might
result be the same as df?Solution
Step 1: Check the code correctness
The code correctly creates a DataFrame and appliesapplymap()with a lambda that adds 1.Step 2: Understand the result assignment
The result is stored inresult, which will be different fromdfbecause each cell is incremented by 1.Final Answer:
The code is correct, but the result is not assigned back to df. -> Option DQuick Check:
applymap() works and returns new DataFrame [OK]
- Expecting applymap to modify df in place
- Thinking applymap fails on numeric data
- Confusing assignment of result with in-place change
df with mixed data types:A B 0 1 foo 1 2 bar 2 3 baz
You want to convert all numeric cells to their squares but leave non-numeric cells unchanged. Which
applymap() function achieves this?Solution
Step 1: Identify the requirement
We want to square numeric values only and keep strings unchanged.Step 2: Analyze each option
df.applymap(lambda x: x**2 if isinstance(x, (int, float)) else x) checks if the cell is int or float, then squares it; else returns original. df.applymap(lambda x: x**2) squares all cells, causing error on strings. df.applymap(lambda x: str(x)) converts all to strings, losing numeric info. df.applymap(lambda x: x**2 if isinstance(x, str) else x) squares strings (causing TypeError) and leaves numerics unchanged.Final Answer:
df.applymap(lambda x: x**2 if isinstance(x, (int, float)) else x) -> Option AQuick Check:
Use type check inside applymap to selectively transform [OK]
- Applying math operations to strings causing errors
- Not checking data type before transformation
- Converting all data to string losing numeric info
Ready to prove it?
Pass the test and this pattern moves unknown
