Bird
Raised Fist0
Pandasdata~20 mins

NaN and None in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NaN and null Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of DataFrame with NaN and null
What is the output of this code snippet?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, None, 3], 'B': [np.nan, 2, 3]})
print(df)
A
     A    B
0  1    NaN
1  null  2
2  3    3
B
     A    B
0  1.0  NaN
1  null  2.0
2  3.0  3.0
C
     A    B
0  1.0  NaN
1  NaN  2.0
2  3.0  3.0
D
     A    B
0  1    NaN
1  NaN  2
2  3    3
Attempts:
2 left
💡 Hint
Remember that null in numeric columns is converted to NaN.
❓ data_output
intermediate
2:00remaining
Count of missing values with isna()
What is the output of this code?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'X': [None, np.nan, 5], 'Y': [1, 2, None]})
print(df.isna().sum())
A
X    2
Y    1
dtype: int64
B
X    2
Y    2
dtype: int64
C
X    1
Y    1
dtype: int64
D
X    1
Y    2
dtype: int64
Attempts:
2 left
💡 Hint
Both null and np.nan count as missing values.
🔧 Debug
advanced
2:30remaining
Why does this fillna not replace null?
Consider this code: import pandas as pd df = pd.DataFrame({'A': [1, None, 3], 'B': ['x', None, 'z']}) df['B'] = df['B'].fillna('missing') print(df) Why does the null in column 'B' get replaced but not in column 'A'?
ABecause fillna only works on string columns, not numeric columns.
BBecause column 'A' is numeric and null is converted to NaN, fillna works only on object columns like 'B'.
CBecause null in numeric columns is ignored by fillna.
DBecause fillna cannot replace null values at all.
Attempts:
2 left
💡 Hint
Think about data types and how pandas treats null in numeric vs object columns.
🚀 Application
advanced
2:30remaining
Filtering rows with null and NaN
Given this DataFrame: import pandas as pd import numpy as np df = pd.DataFrame({'col1': [1, None, 3, np.nan], 'col2': ['a', 'b', None, 'd']}) Which code filters rows where 'col1' is missing (null or NaN)?
Adf[df['col1'].notna()]
Bdf[df['col1'] == None]
Cdf[df['col1'] == np.nan]
Ddf[df['col1'].isna()]
Attempts:
2 left
💡 Hint
Use the pandas function that detects all missing values.
🧠 Conceptual
expert
3:00remaining
Difference between null and NaN in pandas
Which statement correctly describes the difference between null and NaN in pandas?
Anull is a Python singleton representing missing data, NaN is a special floating-point value representing missing numerical data; pandas converts null to NaN in numeric columns.
Bnull and NaN are identical in pandas and always interchangeable without any conversion.
CNaN is used only in string columns, null only in numeric columns in pandas.
Dnull is a floating-point value, NaN is a Python object representing missing data.
Attempts:
2 left
💡 Hint
Think about data types and how pandas stores missing values internally.

Practice

(1/5)
1. What do NaN and None represent in a pandas DataFrame?
easy
A. Missing or null data values
B. Valid numeric data
C. Column headers
D. Index labels

Solution

  1. Step 1: Understand the role of NaN and None in pandas

    Both NaN and None are used to mark missing or null data in pandas DataFrames.
  2. Step 2: Differentiate from other DataFrame elements

    They do not represent valid data, headers, or indexes but indicate absence of data.
  3. Final Answer:

    Missing or null data values -> Option A
  4. Quick Check:

    NaN and None = Missing data [OK]
Hint: NaN and None always mean missing data in pandas [OK]
Common Mistakes:
  • Thinking NaN is a valid number
  • Confusing None with column names
  • Assuming NaN is an index label
2. Which of the following is the correct way to check for missing values in a pandas DataFrame df?
easy
A. df.isna()
B. df.notna()
C. df.isnan()
D. df.ismissing()

Solution

  1. Step 1: Recall pandas functions for missing data

    The correct function to check for missing values is isna(), which returns a boolean DataFrame indicating missing entries.
  2. Step 2: Identify incorrect options

    notna() returns a boolean DataFrame indicating non-missing entries; isnan() and ismissing() do not exist in pandas.
  3. Final Answer:

    df.isna() -> Option A
  4. Quick Check:

    Use isna() to find missing values [OK]
Hint: Use df.isna() to find missing values in pandas [OK]
Common Mistakes:
  • Using non-existent df.isnan()
  • Using df.notna() which checks non-missing values
  • Trying df.ismissing() which is invalid
3. Given the code:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3], 'B': [None, 2, 3]})
result = df.isna().sum().to_dict()

What is the value of result?
medium
A. {'A': 0, 'B': 0}
B. {'A': 0, 'B': 1}
C. {'A': 1, 'B': 0}
D. {'A': 1, 'B': 1}

Solution

  1. Step 1: Identify missing values in each column

    Column 'A' has one np.nan (missing), column 'B' has one None (missing).
  2. Step 2: Use isna() and sum() to count missing per column

    df.isna() marks missing as True, sum() counts Trues per column, resulting in {'A': 1, 'B': 1}.
  3. Final Answer:

    {'A': 1, 'B': 1} -> Option D
  4. Quick Check:

    Count missing with isna().sum() = {'A':1,'B':1} [OK]
Hint: Count missing with df.isna().sum() per column [OK]
Common Mistakes:
  • Ignoring None as missing in column B
  • Confusing sum() to count non-missing
  • Assuming np.nan and None are treated differently here
4. Consider this code snippet:
import pandas as pd
df = pd.DataFrame({'X': [1, None, 3]})
df['X'] = df['X'].fillna(0)
print(df)

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

Solution

  1. Step 1: Identify missing value in column 'X'

    The second row has None, which pandas treats as missing (NaN).
  2. Step 2: Use fillna(0) to replace missing with zero

    The fillna(0) replaces the missing value with 0, so the DataFrame becomes 1, 0, 3.
  3. Final Answer:

    X 0 1 1 0 2 3 -> Option C
  4. Quick Check:

    fillna(0) replaces None with 0 [OK]
Hint: fillna(0) replaces missing values with zero [OK]
Common Mistakes:
  • Expecting None to remain after fillna
  • Confusing fillna with dropna
  • Assuming fillna changes non-missing values
5. You have a DataFrame df with columns 'A' and 'B' containing some NaN and None values. You want to replace all missing values in 'A' with the mean of 'A' and all missing values in 'B' with the string 'missing'. Which code correctly does this?
hard
A. df.fillna({'A': df['A'].mean(), 'B': 'missing'})
B. df['A'] = df['A'].fillna(df['A'].mean()) df['B'] = df['B'].fillna('missing')
C. df['A'].fillna(df['A'].mean()) df['B'].fillna('missing')
D. df.fillna(df.mean()) df['B'] = df['B'].fillna('missing')

Solution

  1. Step 1: Replace missing in 'A' with its mean

    Use df['A'].fillna(df['A'].mean()) and assign back to 'A' to update the column.
  2. Step 2: Replace missing in 'B' with 'missing'

    Use df['B'].fillna('missing') and assign back to 'B' to update that column.
  3. Step 3: Check other options for correctness

    df.fillna({'A': df['A'].mean(), 'B': 'missing'}) does not assign back, so changes are not saved; df['A'].fillna(df['A'].mean()) df['B'].fillna('missing') also misses assignment; df.fillna(df.mean()) df['B'] = df['B'].fillna('missing') tries to fill numeric mean for all columns which fails for 'B'.
  4. Final Answer:

    df['A'] = df['A'].fillna(df['A'].mean()) df['B'] = df['B'].fillna('missing') -> Option B
  5. Quick Check:

    Assign fillna results back to columns [OK]
Hint: Assign fillna results back to columns to save changes [OK]
Common Mistakes:
  • Not assigning fillna result back to DataFrame
  • Using fillna without specifying columns
  • Trying to fill numeric mean into string column
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