Bird
Raised Fist0
Pandasdata~15 mins

Filling missing values with fillna() in Pandas - Deep Dive

Choose your learning style10 modes available
Overview - Filling missing values with fillna()
What is it?
Filling missing values with fillna() is a way to replace empty or missing spots in data tables with meaningful values. In data, missing values can cause problems for analysis or calculations. The fillna() function in pandas helps fix this by filling those gaps with numbers, text, or other data you choose. This makes the data complete and ready for use.
Why it matters
Without filling missing values, data analysis can give wrong answers or fail completely. Missing data can hide important patterns or cause errors in calculations. Using fillna() helps keep data clean and trustworthy, so decisions based on data are better. It saves time and effort by automating the fixing of missing spots instead of manual editing.
Where it fits
Before learning fillna(), you should understand what missing data is and how pandas DataFrames work. After mastering fillna(), you can learn more advanced data cleaning methods like interpolation or using machine learning to guess missing values. Filling missing data is a key step in the data cleaning and preparation phase of any data science project.
Mental Model
Core Idea
fillna() replaces missing spots in data with chosen values to make the data complete and usable.
Think of it like...
Imagine a calendar with some days left blank. Using fillna() is like filling those blank days with planned activities so the calendar is full and useful.
DataFrame with missing values:
┌─────┬───────┬───────┐
│ ID  │ Age   │ Score │
├─────┼───────┼───────┤
│ 1   │ 25    │ 88    │
│ 2   │ NaN   │ 92    │
│ 3   │ 30    │ NaN   │
│ 4   │ NaN   │ NaN   │
└─────┴───────┴───────┘

After fillna(0):
┌─────┬───────┬───────┐
│ ID  │ Age   │ Score │
├─────┼───────┼───────┤
│ 1   │ 25    │ 88    │
│ 2   │ 0     │ 92    │
│ 3   │ 30    │ 0     │
│ 4   │ 0     │ 0     │
└─────┴───────┴───────┘
Build-Up - 7 Steps
1
FoundationUnderstanding missing data basics
🤔
Concept: What missing data means and why it appears in datasets.
Data can have missing spots called NaN (Not a Number) or None. These happen when data wasn't collected, lost, or doesn't apply. Missing data can cause errors or wrong results if not handled.
Result
You recognize missing values in your data and why they matter.
Understanding missing data is the first step to cleaning and preparing data for analysis.
2
FoundationIntroduction to pandas DataFrames
🤔
Concept: How data is stored in tables called DataFrames in pandas.
A DataFrame is like a spreadsheet with rows and columns. Each column can have numbers, text, or missing values. pandas is a tool that helps work with DataFrames easily.
Result
You can load and view data with missing values in pandas.
Knowing DataFrames lets you see where missing data lives and how to fix it.
3
IntermediateBasic fillna() usage to replace missing values
🤔Before reading on: do you think fillna() changes the original data or returns a new copy? Commit to your answer.
Concept: Using fillna() to replace missing values with a fixed value.
You can call fillna(value) on a DataFrame or column to replace all missing spots with 'value'. For example, fillna(0) replaces all NaNs with zero. By default, fillna() returns a new DataFrame and does not change the original unless you use inplace=True.
Result
Missing values are replaced with the chosen value, making data complete.
Knowing fillna() returns a new object by default helps avoid accidental data loss.
4
IntermediateFilling missing values differently per column
🤔Before reading on: can fillna() fill different columns with different values in one call? Commit to yes or no.
Concept: Using a dictionary to specify different fill values for each column.
You can pass a dictionary to fillna() where keys are column names and values are what to fill. For example, fillna({'Age': 0, 'Score': 50}) fills missing Age with 0 and Score with 50. This lets you customize filling based on data meaning.
Result
Each column's missing values are filled with appropriate values in one step.
Customizing fill values per column improves data quality by respecting each column's context.
5
IntermediateUsing method parameter for forward/backward fill
🤔Before reading on: does method='ffill' fill missing values with the next or previous known value? Commit to your answer.
Concept: Filling missing values by copying nearby known values using 'ffill' or 'bfill'.
fillna() can fill missing spots by carrying forward the last known value (method='ffill') or backward the next known value (method='bfill'). This is useful for time series or ordered data where nearby values make sense to fill gaps.
Result
Missing values are filled with nearby existing values instead of fixed constants.
Using forward or backward fill preserves data trends better than fixed values in some cases.
6
AdvancedLimit parameter to control fill extent
🤔Before reading on: does setting limit=1 fill all missing values or only one per column? Commit to your answer.
Concept: Using limit to restrict how many consecutive missing values get filled.
fillna() has a limit parameter that stops filling after a set number of consecutive NaNs. For example, limit=1 fills only the first missing value in a sequence, leaving others untouched. This helps avoid overfilling and keeps some gaps for special handling.
Result
Only a controlled number of missing values are filled, preserving some missing data.
Knowing how to limit filling prevents hiding important missing data patterns.
7
Expertfillna() with inplace and chained operations pitfalls
🤔Before reading on: does inplace=True always modify the original DataFrame safely? Commit to yes or no.
Concept: Understanding how inplace=True works and risks with chained indexing.
Using inplace=True modifies the original DataFrame but can cause unexpected bugs, especially with chained indexing like df['col'][mask].fillna(inplace=True). This can lead to warnings or no change. Best practice is to assign the result back or avoid chained calls.
Result
You avoid subtle bugs and data inconsistencies when filling missing values.
Knowing inplace=True's behavior helps write safer, more predictable data cleaning code.
Under the Hood
fillna() scans the DataFrame for missing values (NaN or None). It then replaces these spots with the specified fill value or uses methods like forward fill by copying nearby values. Internally, pandas uses optimized C code to quickly locate and replace missing entries without copying the entire data unless needed. The inplace parameter controls whether changes happen on the original data or a copy.
Why designed this way?
fillna() was designed to be flexible and efficient for many data types and use cases. It supports scalar fills, dictionary fills per column, and method fills to cover common scenarios. The choice to return a new object by default avoids accidental data loss, while inplace=True offers convenience with caution. This design balances safety, speed, and usability.
┌───────────────┐
│ DataFrame    │
│ with NaNs    │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ fillna() call │
│ with params   │
└──────┬────────┘
       │
       ▼
┌─────────────────────────────┐
│ Identify missing values (NaN)│
│ For each missing spot:       │
│ - Replace with fill value    │
│ - Or copy nearby value       │
└──────┬──────────────────────┘
       │
       ▼
┌───────────────┐
│ New or updated│
│ DataFrame     │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does fillna() change the original DataFrame by default? Commit to yes or no.
Common Belief:fillna() always changes the original DataFrame directly.
Tap to reveal reality
Reality:By default, fillna() returns a new DataFrame and does not modify the original unless inplace=True is set.
Why it matters:Assuming fillna() changes data in place can cause confusion and bugs when the original data remains unchanged.
Quick: Can fillna() fill missing values differently for each column in one call? Commit to yes or no.
Common Belief:fillna() can only fill all missing values with the same value across the entire DataFrame.
Tap to reveal reality
Reality:fillna() accepts a dictionary to fill different columns with different values in one call.
Why it matters:Not knowing this limits data cleaning flexibility and may lead to inefficient multiple calls.
Quick: Does method='ffill' fill missing values with the next known value? Commit to yes or no.
Common Belief:method='ffill' fills missing values with the next known value in the data.
Tap to reveal reality
Reality:method='ffill' fills missing values by carrying forward the previous known value, not the next.
Why it matters:Misunderstanding this can lead to incorrect data filling and wrong analysis results.
Quick: Does setting limit=1 fill all missing values in a sequence? Commit to yes or no.
Common Belief:limit=1 fills all missing values in a sequence of NaNs.
Tap to reveal reality
Reality:limit=1 fills only the first missing value in each consecutive sequence, leaving others untouched.
Why it matters:Misusing limit can cause unexpected partial filling and data inconsistencies.
Expert Zone
1
fillna() works differently on different data types; for example, filling missing categorical data requires careful choice of fill value to avoid type errors.
2
Using inplace=True can cause chained assignment warnings and subtle bugs; assigning the result back is safer and more predictable.
3
fillna() does not change the data type of columns unless the fill value forces a type change, which can cause unexpected behavior.
When NOT to use
fillna() is not suitable when missing data needs to be estimated based on patterns or models. In such cases, interpolation methods or predictive imputation using machine learning should be used instead.
Production Patterns
In real-world pipelines, fillna() is often used early to handle missing data quickly, with different fill values per column based on domain knowledge. It is combined with validation steps to ensure filling does not distort data meaning.
Connections
Data Imputation
fillna() is a simple form of data imputation, which includes more advanced methods like interpolation and model-based filling.
Understanding fillna() lays the foundation for grasping more complex imputation techniques that improve data quality.
Time Series Analysis
fillna() with method='ffill' or 'bfill' is commonly used in time series to fill missing timestamps with nearby values.
Knowing how fillna() works helps maintain continuity in time series data, crucial for accurate forecasting.
Error Handling in Software Engineering
fillna() parallels default value assignment in programming to handle missing or null inputs gracefully.
Recognizing this connection helps appreciate fillna() as a data-level error handling technique, improving robustness.
Common Pitfalls
#1Assuming fillna() changes the original DataFrame without inplace=True.
Wrong approach:df.fillna(0) print(df)
Correct approach:df = df.fillna(0) print(df)
Root cause:Not understanding that fillna() returns a new DataFrame by default and does not modify in place.
#2Using fillna() with inplace=True on a chained indexing operation.
Wrong approach:df['Age'][mask].fillna(0, inplace=True)
Correct approach:df.loc[mask, 'Age'] = df.loc[mask, 'Age'].fillna(0)
Root cause:Chained indexing returns a copy, so inplace=True does not affect the original DataFrame, causing silent failures.
#3Filling all columns with the same value without considering data types.
Wrong approach:df.fillna(0, inplace=True)
Correct approach:df.fillna({'Age': 0, 'Name': 'Unknown'}, inplace=True)
Root cause:Ignoring column data types can cause type errors or meaningless fills.
Key Takeaways
fillna() is a powerful tool to replace missing data with meaningful values, making datasets complete and ready for analysis.
By default, fillna() returns a new DataFrame and does not modify the original unless inplace=True is used.
You can fill missing values differently per column by passing a dictionary to fillna(), improving data cleaning precision.
Using method='ffill' or 'bfill' fills missing values by copying nearby known values, useful for ordered data like time series.
Be cautious with inplace=True and chained indexing to avoid subtle bugs and ensure your data changes as expected.

Practice

(1/5)
1. What does the fillna() function do in pandas?
easy
A. It changes the data type of columns.
B. It removes all rows with missing values.
C. It sorts the DataFrame by column values.
D. It replaces missing values with specified values.

Solution

  1. Step 1: Understand the purpose of fillna()

    The fillna() function is designed to handle missing data by replacing NaN values with a specified value or method.
  2. Step 2: Compare options with fillna() behavior

    Options B, C, and D describe other DataFrame operations, but only It replaces missing values with specified values. correctly describes filling missing values.
  3. Final Answer:

    It replaces missing values with specified values. -> Option D
  4. Quick Check:

    fillna() = replace missing values [OK]
Hint: fillna() fills missing spots with your chosen value [OK]
Common Mistakes:
  • Confusing fillna() with dropna() which removes rows
  • Thinking fillna() sorts data
  • Assuming fillna() changes data types
2. Which of the following is the correct syntax to fill missing values in a DataFrame df with zero using fillna()?
easy
A. df.fillna(na=0)
B. df.fillna = 0
C. df.fillna(0)
D. df.fillna(0, value=0)

Solution

  1. Step 1: Recall fillna() method syntax

    The correct way to fill missing values is calling fillna() with the value as the first argument, like df.fillna(0).
  2. Step 2: Evaluate each option

    df.fillna = 0 tries to assign to fillna which is incorrect. df.fillna(na=0) uses a wrong keyword argument. df.fillna(0, value=0) passes 0 both positionally (as value) and explicitly as value=0 keyword, causing TypeError: multiple values for argument 'value'. Only C is correct.
  3. Final Answer:

    df.fillna(0) -> Option C
  4. Quick Check:

    fillna(value) fills missing with value [OK]
Hint: Use fillna(value) to fill missing values simply [OK]
Common Mistakes:
  • Assigning fillna instead of calling it as a method
  • Using wrong argument names like na=0
  • Confusing inplace parameter usage
3. Given the DataFrame:
import pandas as pd
df = pd.DataFrame({'A': [1, None, 3], 'B': [None, 2, None]})
filled = df.fillna({'A': 0, 'B': 5})
print(filled)

What is the output?
medium
A. A B 0 1 5 1 0 2 2 3 5
B. A B 0 1 NaN 1 0 2 2 3 NaN
C. A B 0 1 0 1 0 2 2 3 0
D. A B 0 0 5 1 0 2 2 0 5

Solution

  1. Step 1: Understand fillna() with dict argument

    Using a dictionary with fillna() fills missing values in each column with the corresponding value from the dict. Here, column 'A' missing values become 0, and column 'B' missing values become 5.
  2. Step 2: Apply fillna() to each column

    Original df has NaN at df.loc[1, 'A'] and df.loc[0, 'B'], df.loc[2, 'B']. After fillna, these become 0 for 'A' and 5 for 'B'. The rest of the values remain unchanged.
  3. Final Answer:

    A B 0 1 5 1 0 2 2 3 5 -> Option A
  4. Quick Check:

    fillna(dict) fills columns separately [OK]
Hint: Use dict in fillna() to fill columns differently [OK]
Common Mistakes:
  • Expecting fillna() to fill all columns with same value
  • Confusing NaN replacement with zero by default
  • Misreading which values are replaced
4. What is wrong with this code snippet?
import pandas as pd
df = pd.DataFrame({'X': [None, 2, None]})
df.fillna(inplace=True, 0)
print(df)
medium
A. fillna() keyword arguments are incorrectly used.
B. The arguments to fillna() are in wrong order.
C. The value argument should come before inplace.
D. fillna() does not have an inplace parameter.

Solution

  1. Step 1: Check fillna() parameter names and order

    In Python functions, all positional arguments must come before any keyword arguments.
  2. Step 2: Identify incorrect argument order

    The code uses inplace=True (keyword) followed by 0 (positional for value), which raises SyntaxError: positional argument follows keyword argument. Correct is df.fillna(0, inplace=True).
  3. Final Answer:

    The arguments to fillna() are in wrong order. -> Option B
  4. Quick Check:

    Positional args before keywords [OK]
Hint: Pass value first, then inplace as keyword in fillna() [OK]
Common Mistakes:
  • Swapping argument order without keywords
  • Using inplace without value argument
  • Confusing positional and keyword arguments
5. You have a DataFrame df with columns 'Score' and 'Grade'. 'Score' has missing values. You want to fill missing 'Score' values with the mean of 'Score' and then fill missing 'Grade' values with 'Unknown'. Which code correctly does this?
hard
A. df.fillna({'Score': df['Score'].mean(), 'Grade': 'Unknown'}, inplace=True)
B. df['Score'].fillna(df['Score'].mean()) df['Grade'].fillna('Unknown')
C. df.fillna({'Score': 'Unknown', 'Grade': df['Grade'].mean()}, inplace=True)
D. df.fillna(df.mean(), inplace=True) df['Grade'].fillna('Unknown')

Solution

  1. Step 1: Calculate mean and prepare fill dictionary

    We calculate the mean of 'Score' using df['Score'].mean() and want to fill missing 'Score' with this mean. For 'Grade', missing values should be filled with 'Unknown'.
  2. Step 2: Use fillna() with a dictionary and inplace=True

    Using df.fillna({'Score': mean_value, 'Grade': 'Unknown'}, inplace=True) fills missing values in both columns correctly in one step.
  3. Step 3: Evaluate other options

    df['Score'].fillna(df['Score'].mean()) df['Grade'].fillna('Unknown') does not assign back the filled columns, so missing values remain. df.fillna({'Score': 'Unknown', 'Grade': df['Grade'].mean()}, inplace=True) swaps fill values incorrectly. df.fillna(df.mean(), inplace=True) df['Grade'].fillna('Unknown') fills 'Score' with its column mean (assuming 'Score' numeric) but the second line returns a new Series without assignment or inplace=True, leaving 'Grade' unchanged.
  4. Final Answer:

    df.fillna({'Score': df['Score'].mean(), 'Grade': 'Unknown'}, inplace=True) -> Option A
  5. Quick Check:

    Use dict with fillna() and inplace=True for multiple columns [OK]
Hint: Fill multiple columns at once with dict and inplace=True [OK]
Common Mistakes:
  • Not assigning back after fillna() without inplace
  • Mixing fill values between columns
  • Using fillna() separately without inplace or assignment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard