Data Science with Python Certification Data Science with Python Data Cleaning and Preparation 3 — Questions and Answers
Question 1: What does `df.duplicated(keep='last')` return?
- True for all duplicates except the last occurrence (Correct answer)
- True for all duplicates except the first occurrence
- True for every duplicate row including the last
- False for every row
Correct answer: True for all duplicates except the last occurrence
keep='last' marks all duplicate occurrences as True except the final one, which is kept.
Question 2: Which technique is most appropriate for detecting outliers in a non-normally distributed dataset?
- IQR (Interquartile Range) method (Correct answer)
- Z-score method
- Grubbs' test
- Dixon's Q test
Correct answer: IQR (Interquartile Range) method
The IQR method is robust to skewed distributions because it relies on quartiles rather than mean and standard deviation.
Question 3: When using `pd.get_dummies()`, what does setting `dtype=int` accomplish?
- Outputs 0/1 integers instead of True/False booleans (Correct answer)
- Encodes ordinal categories as integers
- Converts all columns to integer type
- Applies label encoding
Correct answer: Outputs 0/1 integers instead of True/False booleans
By default get_dummies returns bool dtype in newer pandas; dtype=int forces 0/1 integer output.
Question 4: What is the purpose of `sklearn.preprocessing.RobustScaler`?
- Scales features using statistics that are robust to outliers (median and IQR) (Correct answer)
- Normalizes features to unit norm
- Clips values to a fixed range
- Removes outlier rows before scaling
Correct answer: Scales features using statistics that are robust to outliers (median and IQR)
RobustScaler centers data on the median and scales by IQR, reducing the influence of outliers compared to StandardScaler.
Question 5: Which pandas method efficiently applies a custom cleaning function to every element in a Series?
- Series.map() (Correct answer)
- Series.apply()
- Series.transform()
- Series.applymap()
Correct answer: Series.map()
Series.map() applies a function element-wise to a Series and is the idiomatic pandas choice for element-level transformation.
Question 6: You join two DataFrames and end up with columns 'price_x' and 'price_y'. What caused this?
- Both DataFrames had a column named 'price' and suffixes were added automatically (Correct answer)
- A merge conflict occurred and pandas duplicated the column
- The column types were incompatible
- The join key was named 'price'
Correct answer: Both DataFrames had a column named 'price' and suffixes were added automatically
When both DataFrames share a non-key column name, pandas appends _x and _y suffixes to distinguish them.
Question 7: What does the `errors='coerce'` argument in `pd.to_numeric()` do?
- Converts unparseable values to NaN instead of raising an error (Correct answer)
- Skips unparseable rows silently
- Raises a warning but continues
- Rounds non-numeric strings to zero
Correct answer: Converts unparseable values to NaN instead of raising an error
errors='coerce' forces invalid parsing to produce NaN, making it easy to identify and handle dirty numeric data.
What does `df.duplicated(keep='last')` return?