0
0
Pandasdata~20 mins

NaN and None in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 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.