Data Science with Python Certification Data Analysis with Python 4 — Questions and Answers
Question 1: Which pandas merge type returns only rows that have matching keys in both DataFrames?
- left
- right
- outer
- inner (Correct answer)
Correct answer: inner
An inner join (how='inner') keeps only the intersection — rows whose keys exist in both DataFrames.
Question 2: What does df.rolling(window=7).mean() compute?
- The 7-period simple moving average for each column (Correct answer)
- The cumulative mean over the first 7 rows only
- The mean of every 7th row
- A 7-bin histogram of each column
Correct answer: The 7-period simple moving average for each column
rolling(7).mean() calculates a sliding 7-observation window average, shifting one row at a time.
Question 3: Which method detects duplicate rows in a pandas DataFrame and returns a boolean Series?
- df.is_duplicate()
- df.duplicated() (Correct answer)
- df.find_duplicates()
- df.check_duplicates()
Correct answer: df.duplicated()
df.duplicated() returns True for each row that is an exact duplicate of an earlier row.
Question 4: What is the output dtype of np.array([1, 2.0, 3])?
- int64
- float64 (Correct answer)
- object
- complex128
Correct answer: float64
NumPy upcasts the entire array to float64 because 2.0 is a float and int is a subtype of float.
Question 5: Which pandas accessor is used to perform string operations on a Series of strings?
- .text
- .str (Correct answer)
- .string
- .char
Correct answer: .str
The .str accessor exposes vectorized string functions like .str.upper(), .str.split(), and .str.replace().
Question 6: How does df.query("age > 30 and city == 'NYC'") differ from boolean indexing?
- query() is slower for large DataFrames
- query() accepts a string expression and can reference column names directly without df['col'] (Correct answer)
- query() modifies the DataFrame in place
- query() only works on integer columns
Correct answer: query() accepts a string expression and can reference column names directly without df['col']
df.query() evaluates a string expression, allowing cleaner syntax without repeatedly typing df['column_name'].
Question 7: What does np.linalg.lstsq(A, b) solve?
- The exact solution to the system Ax = b
- The least-squares solution minimizing ||Ax - b|| (Correct answer)
- The eigenvalue decomposition of A
- The LU factorization of matrix A
Correct answer: The least-squares solution minimizing ||Ax - b||
lstsq finds x that minimizes the sum of squared residuals ||Ax - b||², useful when the system is overdetermined.
Which pandas merge type returns only rows that have matching keys in both DataFrames?