Bird
Raised Fist0
Pandasdata~20 mins

Filling missing values with fillna() in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Fillna Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of fillna() with a scalar value?
Given the DataFrame below, what will be the result after filling missing values with 0 using fillna(0)?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3], 'B': [4, 5, np.nan]})
result = df.fillna(0)
print(result)
A{'A': [1.0, 0.0, 3.0], 'B': [4.0, 5.0, 0.0]}
B{'A': [1, nan, 3], 'B': [4, 5, nan]}
C{'A': [1.0, nan, 3.0], 'B': [4.0, 5.0, nan]}
D{'A': [1, 0, 3], 'B': [4, 5, 0]}
Attempts:
2 left
💡 Hint
Remember that fillna replaces all NaN values with the given scalar.
❓ data_output
intermediate
2:00remaining
How many missing values remain after fillna() with method='ffill'?
Consider the DataFrame below. After applying fillna(method='ffill'), how many missing values remain?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'X': [np.nan, 2, np.nan, 4, np.nan]})
filled = df.fillna(method='ffill')
missing_count = filled.isna().sum().iloc[0]
print(missing_count)
A2
B0
C3
D1
Attempts:
2 left
💡 Hint
Forward fill cannot fill NaN at the start of the column.
🔧 Debug
advanced
2:00remaining
Why does this fillna() call raise an error?
What error will this code raise and why?
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3]})
df.fillna(inplace=True, value=[0, 0])
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3]})
df.fillna(inplace=True, value=[0, 0])
AValueError: Length of values does not match length of index
BTypeError: fillna() got multiple values for argument 'value'
CAttributeError: 'NoneType' object has no attribute 'fillna'
DNo error, fills NaN with 0
Attempts:
2 left
💡 Hint
Check if the length of the list matches the DataFrame length.
🚀 Application
advanced
2:00remaining
Which fillna() call fills missing values with column means?
You want to fill missing values in each column with that column's mean. Which code does this correctly?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3], 'B': [4, 5, np.nan]})
Adf.fillna(df.median())
Bdf.fillna(df.mean())
Cdf.fillna({'A': df['A'].mean(), 'B': df['B'].median()})
Ddf.fillna(0)
Attempts:
2 left
💡 Hint
Use a Series with means for each column as the fill value.
🧠 Conceptual
expert
2:00remaining
What is the effect of fillna() with limit parameter?
Given a column with consecutive NaNs, what does fillna(method='bfill', limit=1) do?
AFills NaNs only if they are at the start of the column
BFills all NaNs in the column with the next valid value
CFills only the first NaN in each consecutive group, leaving others as NaN
DRaises an error because limit cannot be used with method
Attempts:
2 left
💡 Hint
Limit controls how many consecutive NaNs get filled.

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