Challenge - 5 Problems
NaN and null Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that null in numeric columns is converted to NaN.
✗ Incorrect
In pandas, null in a numeric column is converted to NaN, so both null and np.nan appear as NaN in the DataFrame.
❓ data_output
intermediate2: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())
Attempts:
2 left
💡 Hint
Both null and np.nan count as missing values.
✗ Incorrect
Both null and np.nan are treated as missing values by isna(), so column X has 2 missing and Y has 1 missing.
🔧 Debug
advanced2: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'?
Attempts:
2 left
💡 Hint
Think about data types and how pandas treats null in numeric vs object columns.
✗ Incorrect
In numeric columns, null is converted to NaN, so fillna replaces NaN. In object columns, null is a Python null and fillna replaces it directly.
🚀 Application
advanced2: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)?
Attempts:
2 left
💡 Hint
Use the pandas function that detects all missing values.
✗ Incorrect
isna() detects both null and NaN as missing values. Comparing with null or np.nan directly does not work as expected.
🧠 Conceptual
expert3:00remaining
Difference between null and NaN in pandas
Which statement correctly describes the difference between null and NaN in pandas?
Attempts:
2 left
💡 Hint
Think about data types and how pandas stores missing values internally.
✗ Incorrect
null is a Python object for missing data; NaN is a float value for missing numerical data. pandas converts null to NaN in numeric columns for consistency.