Dropping missing values (dropna) in Data Analysis Python - Time & Space Complexity
When we remove missing values from data, we want to know how long it takes as the data grows.
How does the time needed change when we have more rows?
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, None, 4]
})
clean_data = data.dropna()
This code removes all rows that have any missing values from the DataFrame.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each cell in the DataFrame for missing values.
- How many times: Once for every cell (row x column) in the data.
As the number of rows grows, the time to check all cells grows proportionally.
| Input Size (rows) | Approx. Operations (checks) |
|---|---|
| 10 | 10 x columns |
| 100 | 100 x columns |
| 1000 | 1000 x columns |
Pattern observation: The time grows linearly with the number of rows.
Time Complexity: O(n * m)
This means the time to drop missing values grows directly with the number of rows and columns in the data.
[X] Wrong: "Dropping missing values takes the same time no matter how big the data is."
[OK] Correct: The method must check every row and column to find missing values, so more data means more work.
Understanding how data cleaning steps like dropping missing values scale helps you explain your approach clearly and confidently.
"What if we only dropped rows missing values in a single column? How would the time complexity change?"
Practice
dropna() function do in a pandas DataFrame?Solution
Step 1: Understand the purpose of
Thedropna()dropna()function is used to remove missing data (NaN values) from a DataFrame.Step 2: Identify what is removed
It removes rows or columns that contain missing values, depending on parameters.Final Answer:
Removes rows or columns with missing values -> Option DQuick Check:
dropna()removes missing data [OK]
- Confusing dropna with fillna
- Thinking it sorts data
- Assuming it duplicates data
df?Solution
Step 1: Understand axis parameter in
axis=0 means drop rows, axis=1 means drop columns.dropna()Step 2: Identify correct axis for dropping rows
To drop rows with missing values, use axis=0.Final Answer:
df.dropna(axis=0) -> Option CQuick Check:
axis=0 drops rows [OK]
- Using axis=1 to drop rows
- Using invalid axis=2
- Confusing inplace parameter
df below, what will df.dropna() return?import pandas as pd
data = {'A': [1, 2, None], 'B': [4, None, 6]}
df = pd.DataFrame(data)
print(df.dropna())Solution
Step 1: Examine the DataFrame rows for missing values
Row 0: A=1, B=4 (no missing)
Row 1: A=2, B=NaN (missing in B)
Row 2: A=NaN, B=6 (missing in A)Step 2: Apply
By default,dropna()default behaviordropna()drops rows with any missing values, so only row 0 remains.Final Answer:
Rows with index 0 only -> Option AQuick Check:
Only complete rows remain [OK]
- Assuming it drops columns instead of rows
- Thinking it fills missing values
- Expecting multiple rows to remain
df.dropna(axis=2)
Solution
Step 1: Check valid axis values for dropna
Valid axis values are 0 (rows) and 1 (columns). axis=2 is invalid and causes an error.Step 2: Understand error cause
Using axis=2 will raise a ValueError because pandas does not support axis=2 for DataFrames.Final Answer:
axis=2 is invalid; axis should be 0 or 1 -> Option AQuick Check:
axis must be 0 or 1 [OK]
- Using axis=2 by mistake
- Forgetting axis parameter
- Confusing inplace with axis
df with some missing values. You want to drop only those rows where all columns are missing values. Which dropna() parameter should you use?Solution
Step 1: Understand the
howparameter indropna()how='all'drops rows only if all values are missing.how='any'drops rows if any value is missing.Step 2: Identify correct parameter for dropping rows with all missing
Since the goal is to drop rows where all columns are missing, usehow='all'.Final Answer:
dropna(how='all') -> Option BQuick Check:
how='all' drops rows with all NaN [OK]
- Using how='any' which drops too many rows
- Confusing thresh with how
- Dropping columns instead of rows
Ready to prove it?
Pass the test and this pattern moves unknown
