Data Science with Python Certification Data Analysis with Python 2 — Questions and Answers
Question 1: Which pandas method fills forward-propagates missing values along a column?
- fillna(method='bfill')
- fillna(method='ffill') (Correct answer)
- interpolate(method='linear')
- dropna(how='all')
Correct answer: fillna(method='ffill')
ffill (forward fill) propagates the last valid observation forward to replace NaN values.
Question 2: What does df.pivot_table(values='sales', index='region', aggfunc='mean') return?
- A reshaped DataFrame with sales as columns
- Mean sales value per region (Correct answer)
- A sorted DataFrame by sales descending
- A cross-tabulation counting occurrences
Correct answer: Mean sales value per region
pivot_table with aggfunc='mean' computes the mean of the values column grouped by the index.
Question 3: Which NumPy function stacks arrays vertically (row-wise)?
- np.hstack
- np.concatenate(axis=1)
- np.vstack (Correct answer)
- np.column_stack
Correct answer: np.vstack
np.vstack stacks a sequence of arrays along the first (vertical) axis, equivalent to concatenate with axis=0.
Question 4: In pandas, what is the result of df.groupby('dept')['salary'].transform('mean')?
- A Series with one mean value per department
- A Series with each row replaced by its department's mean salary (Correct answer)
- A new DataFrame with department means as columns
- An error because transform requires a lambda
Correct answer: A Series with each row replaced by its department's mean salary
transform broadcasts the group-level aggregation back to the original DataFrame's shape.
Question 5: Which method converts a pandas Series of strings to datetime objects?
- Series.astype('datetime64')
- pd.to_datetime(Series) (Correct answer)
- Series.datetime.parse()
- Series.cast(dtype='datetime')
Correct answer: pd.to_datetime(Series)
pd.to_datetime() is the standard function for parsing strings or numbers into datetime objects.
Question 6: What does the .str.contains() method return when applied to a pandas Series?
- A filtered DataFrame with matching rows
- A boolean Series indicating pattern matches (Correct answer)
- The count of matches per element
- A list of matching substrings
Correct answer: A boolean Series indicating pattern matches
.str.contains() returns a boolean Series that is True where the pattern is found.
Question 7: Which pandas function computes pairwise correlation between all numeric columns?
- df.cov()
- df.corr() (Correct answer)
- df.describe()
- df.corrwith()
Correct answer: df.corr()
df.corr() returns a DataFrame of pairwise Pearson (or other) correlation coefficients between all numeric columns.
Which pandas method fills forward-propagates missing values along a column?