0
0
Data Analysis Pythondata~10 mins

Filling missing values (fillna) in Data Analysis Python - Step-by-Step Execution

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

df = pd.DataFrame({'A': [1, None, 3], 'B': [4, 5, None]})
df_filled = df.fillna(0)
print(df_filled)
This code fills missing values in a DataFrame with 0.
Execution Table
StepDataFrame Statefillna() ActionResulting DataFrame
1{'A': [1, None, 3], 'B': [4, 5, None]}Identify missing values at positions A[1], B[2]{'A': [1, None, 3], 'B': [4, 5, None]}
2Same as step 1Apply fillna(0) to replace None with 0{'A': [1, 0, 3], 'B': [4, 5, 0]}
3No missing values remainPrint filled DataFrame A B 0 1 4 1 0 5 2 3 0
4Execution endsNo more stepsEnd of process
💡 All missing values replaced by 0, no None left in DataFrame
Variable Tracker
VariableStartAfter fillnaFinal
df{'A': [1, None, 3], 'B': [4, 5, None]}No changeSame as start
df_filledNot defined{'A': [1, 0, 3], 'B': [4, 5, 0]}Same as after fillna
Key Moments - 2 Insights
Why does fillna(0) replace None with 0 in the DataFrame?
fillna(0) looks for missing values (None or NaN) and replaces them with 0, as shown in execution_table step 2.
Does fillna() change the original DataFrame?
No, fillna() returns a new DataFrame with filled values. The original stays the same unless you use inplace=True, as seen in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 2, what value replaces the missing value in column 'A'?
ANone
B0
C1
DNaN
💡 Hint
Check the 'Resulting DataFrame' column in step 2 of the execution_table.
At which step does the DataFrame have no missing values?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'DataFrame State' and 'Resulting DataFrame' columns in the execution_table.
If we used fillna(method='ffill') instead of fillna(0), what would happen to the missing value in 'B' at index 2?
AIt would remain None
BIt would be replaced by 0
CIt would be replaced by 5
DIt would be replaced by 4
💡 Hint
Forward fill (ffill) replaces missing values with the previous non-missing value, check variable_tracker for values in 'B'.
Concept Snapshot
fillna(value) replaces missing values in a DataFrame with the given value.
It returns a new DataFrame unless inplace=True is used.
Missing values include None or NaN.
You can also use methods like 'ffill' or 'bfill' to fill.
Useful to clean data before analysis.
Full Transcript
We start with a DataFrame that has missing values represented by None. We identify these missing spots. Then, we choose a value to fill them, here zero. Using fillna(0), we replace all missing values with zero, creating a new DataFrame. The original DataFrame remains unchanged. Finally, we print the filled DataFrame which has no missing values. This process helps prepare data for analysis by removing gaps.