Data Science with Python Certification Data Science with Python Data Cleaning and Preparation 2 — Questions and Answers
Question 1: Which pandas method returns a boolean DataFrame indicating where values are missing?
- df.isna() (Correct answer)
- df.missing()
- df.nulls()
- df.isnull_map()
Correct answer: df.isna()
df.isna() (alias: df.isnull()) returns a boolean DataFrame with True where values are NaN.
Question 2: What does the `thresh` parameter in DataFrame.dropna() control?
- Minimum number of non-NaN values required to keep a row (Correct answer)
- Maximum number of NaN values allowed per column
- Threshold percentage of missing data
- Number of rows to drop
Correct answer: Minimum number of non-NaN values required to keep a row
thresh=N keeps only rows/columns with at least N non-NaN values.
Question 3: You have a column with values ['1,200', '3,400', '500']. Which approach correctly converts these to integers?
- df['col'].str.replace(',', '').astype(int) (Correct answer)
- df['col'].astype(int)
- pd.to_numeric(df['col'])
- df['col'].int()
Correct answer: df['col'].str.replace(',', '').astype(int)
You must strip the comma string characters before casting to int since commas are not valid numeric characters.
Question 4: Which scikit-learn class imputes missing values using the median of each feature column?
- SimpleImputer(strategy='median') (Correct answer)
- MedianImputer()
- SimpleImputer(strategy='mean')
- KNNImputer(metric='median')
Correct answer: SimpleImputer(strategy='median')
SimpleImputer with strategy='median' fills NaN values with the column median.
Question 5: What is the primary risk of using forward fill (ffill) to impute time-series data?
- It propagates stale values across genuinely missing periods (Correct answer)
- It introduces future data leakage
- It converts floats to integers
- It removes timezone information
Correct answer: It propagates stale values across genuinely missing periods
ffill carries the last valid observation forward, which can mask real gaps and bias analysis if the gap is long.
Question 6: Which pandas function is best suited for converting a column of mixed date strings to datetime objects?
- pd.to_datetime() (Correct answer)
- datetime.strptime()
- df['col'].astype('datetime')
- pd.parse_dates()
Correct answer: pd.to_datetime()
pd.to_datetime() handles many date formats automatically and accepts the errors parameter for malformed entries.
Question 7: After one-hot encoding a categorical column with 5 unique values, how many new columns are created to avoid the dummy variable trap?
- 4 (Correct answer)
- 5
- 3
- 6
Correct answer: 4
Dropping one category (drop_first=True) leaves 4 columns, preventing perfect multicollinearity.
Which pandas method returns a boolean DataFrame indicating where values are missing?