Dropping missing values with dropna() in Pandas - Time & Space Complexity
We want to understand how the time to remove missing values grows as the data gets bigger.
How does the work change when we have more rows in our data?
Analyze the time complexity of the following code snippet.
import pandas as pd
data = pd.DataFrame({
'A': [1, 2, None, 4],
'B': [None, 2, 3, 4],
'C': [1, None, 3, 4]
})
clean_data = data.dropna()
This code creates a small table with some missing values and removes all rows that have any missing value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each cell in the table to find missing values.
- How many times: Once for every cell in the data (rows x columns).
As the number of rows grows, the work grows roughly in direct proportion to the number of cells.
| Input Size (rows) | Approx. Operations (cells checked) |
|---|---|
| 10 | 10 x columns |
| 100 | 100 x columns |
| 1000 | 1000 x columns |
Pattern observation: Doubling rows doubles the work, since every cell is checked once.
Time Complexity: O(n * m)
This means the time grows proportionally with the number of rows (n) times the number of columns (m).
[X] Wrong: "dropna() only looks at rows, so time grows with rows only."
[OK] Correct: dropna() must check every cell to find missing values, so columns also affect time.
Understanding how data cleaning steps like dropna() scale helps you explain your code choices clearly and confidently.
"What if we used dropna(axis=1) to drop columns with missing values? How would the time complexity change?"
Practice
dropna() function do in pandas?Solution
Step 1: Understand the purpose of
Thedropna()dropna()function is designed to remove data that contains missing values (NaN) from a DataFrame.Step 2: Identify what
It removes either rows or columns that have missing values, depending on the parameters used.dropna()removesFinal Answer:
Removes rows or columns with missing values -> Option CQuick Check:
dropna()removes missing data [OK]
- Confusing dropna() with fillna()
- Thinking dropna() sorts data
- Assuming dropna() calculates statistics
df?Solution
Step 1: Identify axis for rows
In pandas,axis=0means rows, so to drop rows, useaxis=0.Step 2: Use
Thehow='any'to drop rows with any missing valueshow='any'parameter drops rows if any value is missing in that row.Final Answer:
df.dropna(axis=0, how='any') -> Option AQuick Check:
Drop rows with any NaN = axis=0, how='any' [OK]
- Using axis=1 to drop rows
- Using how='all' to drop rows with any NaN
- Confusing axis and how parameters
import pandas as pd
data = {'A': [1, 2, None], 'B': [4, None, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)What is the result of
df.dropna(axis=0, how='any')?Solution
Step 1: Identify rows with any missing values
Row 0: A=1, B=4, C=7 (no NaN)
Row 1: A=2, B=NaN, C=8 (has NaN)
Row 2: A=NaN, B=6, C=9 (has NaN)Step 2: Drop rows with any NaN
Only row 0 has no missing values, so only it remains after dropna(axis=0, how='any').Final Answer:
Only row with index 0 -> Option DQuick Check:
Drop rows with any NaN leaves only complete rows [OK]
- Keeping rows with NaN
- Confusing axis=0 and axis=1
- Misreading how='any' vs how='all'
df.dropna(axis=2)
and get an error. What is the problem?
Solution
Step 1: Understand valid axis values for dropna()
In pandas,axiscan only be 0 (rows) or 1 (columns) for dropna().Step 2: Identify the error cause
Usingaxis=2is invalid and causes an error because it is not a supported axis.Final Answer:
axis=2 is invalid; only 0 (rows) or 1 (columns) allowed -> Option AQuick Check:
Valid axis values = 0 or 1 only [OK]
- Using axis=2 mistakenly
- Thinking dropna() needs fillna() first
- Assuming how parameter is mandatory
df with some rows having all values missing and some rows having only some missing values. Which dropna() call will remove only rows where all values are missing, keeping rows with partial data?Solution
Step 1: Understand
Usinghow='all'parameterhow='all'drops rows only if all values in that row are missing.Step 2: Use
Since we want to drop rows,axis=0to drop rowsaxis=0is correct.Step 3: Confirm this keeps rows with some data
Rows with some non-missing values are kept because not all values are missing.Final Answer:
df.dropna(axis=0, how='all') -> Option BQuick Check:
Drop rows with all NaN = axis=0, how='all' [OK]
- Using how='any' which drops partial rows
- Using axis=1 which drops columns
- Confusing how parameter effects
Ready to prove it?
Pass the test and this pattern moves unknown
