0
0
Pandasdata~10 mins

Filling missing values with fillna() in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Filling missing values with fillna()
Start with DataFrame
Identify missing values
Choose fill value or method
Apply fillna()
Get DataFrame with no missing values
End
Start with a DataFrame that has missing values, decide how to fill them, apply fillna(), and get a DataFrame without missing values.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, None, 3], 'B': [None, 2, 3]})
df_filled = df.fillna(0)
print(df_filled)
This code creates a DataFrame with missing values and fills them with 0 using fillna().
Execution Table
StepDataFrame Statefillna() ArgumentActionResulting DataFrame
1{'A': [1, None, 3], 'B': [None, 2, 3]}0Identify missing values in columns A and B{'A': [1, None, 3], 'B': [None, 2, 3]} (missing values present)
2Same as above0Apply fillna(0) to replace all None with 0{'A': [1, 0, 3], 'B': [0, 2, 3]} (missing values replaced)
3Filled DataFrameN/APrint the filled DataFrame A B 0 1.0 0.0 1 0.0 2.0 2 3.0 3.0
4Filled DataFrameN/AEnd of executionNo missing values remain
💡 All missing values replaced by 0, no None left in DataFrame
Variable Tracker
VariableStartAfter fillna(0)Final
df{'A': [1, None, 3], 'B': [None, 2, 3]}{'A': [1, None, 3], 'B': [None, 2, 3]}Unchanged
df_filledNot defined{'A': [1, 0, 3], 'B': [0, 2, 3]}Filled DataFrame with no missing values
Key Moments - 3 Insights
Why does df not change after fillna(0)?
Because fillna() returns a new DataFrame by default and does not modify df in place. See execution_table step 2 where df remains unchanged.
What happens if we use fillna() without an argument?
fillna() requires a value or method to fill missing data. Without an argument, it will raise an error. This is why we specify 0 in step 2.
Can fillna() fill missing values differently for each column?
Yes, by passing a dictionary with column names as keys and fill values as values. This is not shown here but is a common use.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 2. What value replaces the missing values?
A1
B0
CNone
DNaN
💡 Hint
Check the 'fillna() Argument' and 'Resulting DataFrame' columns at step 2.
At which step does the DataFrame have no missing values?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Resulting DataFrame' column to see when missing values are replaced.
If we want to change df directly without creating df_filled, what should we do?
AUse df.fillna(0) without assignment
BUse df.fillna() without arguments
CUse df.fillna(0, inplace=True)
DUse df.fillna(0).copy()
💡 Hint
Recall that fillna() returns a new DataFrame unless inplace=True is used.
Concept Snapshot
fillna() replaces missing values in a DataFrame.
Syntax: df.fillna(value, inplace=False)
By default, returns a new DataFrame.
Use inplace=True to modify original.
Value can be a scalar or dict per column.
Missing values are replaced by the given value.
Full Transcript
We start with a DataFrame that has missing values represented by None. We want to fill these missing spots with a value, here zero. Using fillna(0), we create a new DataFrame where all None values are replaced by 0. The original DataFrame remains unchanged unless we use inplace=True. This method helps clean data by removing missing values in a simple way.