What if you could fix all missing data in seconds instead of hours?
Why Filling missing values (fillna) in Data Analysis Python? - Purpose & Use Cases
Imagine you have a big table of customer data with some empty spots where information is missing. You try to fix it by looking at each empty spot and guessing what should be there.
Doing this by hand is slow and tiring. You might make mistakes or miss some spots. It's hard to keep track and update all missing pieces correctly.
Using fillna lets you quickly fill all missing spots with a value you choose or a calculated one. It works fast and keeps your data clean without errors.
for i in range(len(data)): if data[i] is None: data[i] = 0
data.fillna(0, inplace=True)
You can handle incomplete data easily and prepare it for analysis or machine learning without wasting time.
A store's sales data has missing prices for some products. Filling those missing prices with the average price helps calculate total sales correctly.
Manual filling is slow and error-prone.
fillna fills all missing values quickly and safely.
This makes data ready for better analysis and decisions.
Practice
fillna() function do in a pandas DataFrame?Solution
Step 1: Understand the purpose of fillna()
Thefillna()function is designed to handle missing data by replacing NaN values.Step 2: Identify what fillna() replaces missing values with
It can replace missing values with a fixed value or use methods like forward fill or backward fill.Final Answer:
It replaces missing values with a specified value or method. -> Option BQuick Check:
fillna() replaces NaN values = D [OK]
- Confusing fillna() with dropna() which deletes rows
- Thinking fillna() sorts data
- Assuming fillna() changes data types
df?Solution
Step 1: Identify the correct method call on a DataFrame column
To fill missing values in a specific column, callfillna()on that column likedf['Age'].fillna(30).Step 2: Check syntax correctness
df['Age'].fillna(30) uses correct syntax: column selection with brackets and method call with parentheses and argument.Final Answer:
df['Age'].fillna(30) -> Option DQuick Check:
Correct method call syntax = A [OK]
- Using square brackets instead of parentheses for fillna()
- Calling fillna on DataFrame with wrong syntax
- Using fill() instead of fillna()
df below, what will be the output of df['Score'].fillna(0)?
import pandas as pd
df = pd.DataFrame({'Score': [10, None, 20, None, 30]})
print(df['Score'].fillna(0))Solution
Step 1: Understand fillna(0) effect on NaN values
Callingfillna(0)replaces all missing (NaN) values with 0.Step 2: Apply fillna(0) to the 'Score' column
The original column has values [10, NaN, 20, NaN, 30]. After fillna(0), NaNs become 0, so result is [10, 0, 20, 0, 30].Final Answer:
[10, 0, 20, 0, 30] -> Option CQuick Check:
NaN replaced by 0 = C [OK]
- Expecting fillna() to remove rows
- Thinking fillna() copies previous values by default
- Confusing fillna(0) with fillna(method='ffill')
df?
df.fillna(method='forward')
Solution
Step 1: Check valid method names for fillna()
fillna() supports method='ffill' for forward fill and method='bfill' for backward fill.Step 2: Identify the incorrect method name
The code uses method='forward', which is invalid. The correct name is 'ffill'.Final Answer:
The method name should be 'ffill', not 'forward'. -> Option AQuick Check:
Correct method name = 'ffill' [OK]
- Using 'forward' instead of 'ffill'
- Thinking fillna() can't use methods
- Calling fillna() only on columns, not DataFrame
df with missing values in multiple columns. You want to fill missing values in column 'A' with the mean of 'A', and in column 'B' with the previous valid value. Which code correctly does this?Solution
Step 1: Fill column 'A' with its mean
Calculate mean withdf['A'].mean()and fill missing values usingfillna()on column 'A'. Assign back todf['A'].Step 2: Fill column 'B' with previous valid value
Usefillna(method='ffill')on column 'B' to forward fill missing values. Assign back todf['B'].Final Answer:
df['A'] = df['A'].fillna(df['A'].mean()) df['B'] = df['B'].fillna(method='ffill') -> Option AQuick Check:
Mean fill + forward fill = B [OK]
- Using fillna() without assignment, so no change saved
- Passing 'ffill' as string value instead of method argument
- Trying to use method='mean' which is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
