Data Science with Python Certification Data Science with Python Data Cleaning and Preparation 4 — Questions and Answers
Question 1: Which method removes leading and trailing whitespace from every string in a pandas Series?
- Series.str.strip() (Correct answer)
- Series.strip()
- Series.str.trim()
- Series.str.clean()
Correct answer: Series.str.strip()
The .str accessor exposes vectorized string methods; strip() removes surrounding whitespace.
Question 2: You have 10,000 rows but only 3 rows contain NaN in a non-critical column. What is the most defensible cleaning strategy?
- Drop those 3 rows since the data loss is negligible (Correct answer)
- Impute with the column mean to preserve all rows
- Replace NaN with zero unconditionally
- Flag them and keep as-is for analysis
Correct answer: Drop those 3 rows since the data loss is negligible
When missing data is less than 0.1% of rows, dropping them causes negligible bias and avoids imputation assumptions.
Question 3: What is the correct way to rename columns in pandas without modifying the original DataFrame?
- df.rename(columns={'old': 'new'}, inplace=False) (Correct answer)
- df.columns['old'] = 'new'
- df.rename({'old': 'new'})
- df.set_column('old', 'new')
Correct answer: df.rename(columns={'old': 'new'}, inplace=False)
rename() with inplace=False (the default) returns a new DataFrame with the renamed columns.
Question 4: Which pandas method converts a wide-format DataFrame to long format?
- pd.melt() (Correct answer)
- pd.pivot()
- df.stack()
- df.unstack()
Correct answer: pd.melt()
pd.melt() unpivots a DataFrame from wide to long format, turning column headers into row values.
Question 5: When standardizing column names for a pipeline, which transformation is considered best practice?
- Lowercase, replace spaces with underscores, remove special characters (Correct answer)
- Uppercase all characters
- Use camelCase matching the source system
- Keep original names to preserve provenance
Correct answer: Lowercase, replace spaces with underscores, remove special characters
Snake_case lowercase names are Python-idiomatic, avoid attribute access issues, and work cleanly with SQL and most ML libraries.
Question 6: What does `df.astype({'age': 'int32', 'salary': 'float32'})` accomplish?
- Casts specified columns to smaller numeric types, reducing memory usage (Correct answer)
- Validates that columns match the given types
- Rounds values to fit the target type
- Creates new columns with the specified types
Correct answer: Casts specified columns to smaller numeric types, reducing memory usage
Passing a dict to astype() casts each named column to its specified dtype, which can significantly reduce DataFrame memory footprint.
Question 7: Which approach is preferred to handle a skewed numerical feature before feeding it to a linear model?
- Apply a log transform (np.log1p) to reduce skewness (Correct answer)
- Clip values at the 99th percentile
- Replace with rank values
- Bin the variable into equal-width buckets
Correct answer: Apply a log transform (np.log1p) to reduce skewness
log1p(x) compresses the long tail of right-skewed distributions, bringing the feature closer to normal and improving linear model performance.
Which method removes leading and trailing whitespace from every string in a pandas Series?