The fillna() function replaces missing values (NaN) in a DataFrame or Series with a specified value or method.
Click to reveal answer
beginner
How can you fill missing values with the mean of a column using fillna()?
Calculate the mean of the column and pass it to fillna(). For example: df['col'].fillna(df['col'].mean(), inplace=True).
Click to reveal answer
intermediate
What does the parameter method='ffill' do in fillna()?
It fills missing values by propagating the last valid observation forward to the next missing value.
Click to reveal answer
intermediate
Can fillna() fill missing values differently for each column in a DataFrame?
Yes, by passing a dictionary with column names as keys and fill values as values, e.g., df.fillna({'A': 0, 'B': 1}).
Click to reveal answer
beginner
What happens if you use fillna() without inplace=True?
It returns a new DataFrame or Series with missing values filled, but does not change the original data unless you assign it back or use inplace=True.
Click to reveal answer
What is the default behavior of fillna() if no value or method is specified?
AIt does nothing and returns the original data
BIt fills missing values with zero
CIt raises an error
DIt fills missing values with the mean
✗ Incorrect
If no value or method is specified, fillna() returns the original data unchanged.
Which fillna() method fills missing values using the next valid value?
Amethod='ffill'
Bvalue='mean'
Cvalue=0
Dmethod='bfill'
✗ Incorrect
method='bfill' fills missing values by using the next valid value (backward fill).
How do you fill missing values in multiple columns with different values using fillna()?
APass a list of values
BPass a dictionary with column names and values
CCall <code>fillna()</code> multiple times
DUse <code>method='ffill'</code>
✗ Incorrect
Passing a dictionary like {'col1': val1, 'col2': val2} fills each column with its specified value.
What does inplace=True do in fillna()?
ADeletes missing values
BReturns a copy with filled values
CModifies the original data directly
DRaises an error if missing values exist
✗ Incorrect
inplace=True changes the original DataFrame or Series without needing assignment.
Which of these is NOT a valid way to fill missing values with fillna()?
AUsing <code>fillna()</code> without any arguments
BUsing a method like 'ffill'
CUsing a dictionary for different columns
DUsing a scalar value like 0
✗ Incorrect
fillna() without arguments does not fill missing values and returns data unchanged.
Explain how you would fill missing values in a DataFrame column with the column's median using fillna().
Think about how to get the median and pass it to fillna.
You got /3 concepts.
Describe the difference between using method='ffill' and method='bfill' in fillna().
Consider the direction of filling missing values.
You got /3 concepts.
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
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.
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.
Final Answer:
It replaces missing values with specified values. -> Option D
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
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).
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.
Final Answer:
df.fillna(0) -> Option C
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 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.
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.
Final Answer:
A B
0 1 5
1 0 2
2 3 5 -> Option A
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
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
Step 1: Check fillna() parameter names and order
In Python functions, all positional arguments must come before any keyword arguments.
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).
Final Answer:
The arguments to fillna() are in wrong order. -> Option B
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
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'.
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.
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.
Final Answer:
df.fillna({'Score': df['Score'].mean(), 'Grade': 'Unknown'}, inplace=True) -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.