Data Science with Python Certification Data Analysis with Python 3 — Questions and Answers
Question 1: When reading a CSV with pd.read_csv(), which parameter skips the first two rows of data?
- header=2
- skiprows=2 (Correct answer)
- skip_header=2
- nrows_skip=2
Correct answer: skiprows=2
skiprows accepts an integer or list of row indices to skip before parsing the file.
Question 2: What does np.where(condition, x, y) return?
- Indices where condition is True
- An array with x where condition is True, else y (Correct answer)
- A boolean mask of the condition
- The count of True values in condition
Correct answer: An array with x where condition is True, else y
np.where with three arguments acts as a vectorized ternary operator, selecting from x or y element-wise.
Question 3: Which pandas method reshapes data from wide format to long format?
- df.pivot()
- df.melt() (Correct answer)
- df.stack()
- df.unstack()
Correct answer: df.melt()
df.melt() unpivots a DataFrame from wide to long format, making column headers into values.
Question 4: A DataFrame has a MultiIndex. Which method converts the index levels back to regular columns?
- df.reset_index() (Correct answer)
- df.flatten_index()
- df.unstack(level=0)
- df.to_columns()
Correct answer: df.reset_index()
reset_index() moves all or specified index levels into DataFrame columns and resets to a default integer index.
Question 5: What is the purpose of pd.cut() in data analysis?
- Removes outliers beyond a threshold
- Bins continuous data into discrete intervals (Correct answer)
- Clips values to a specified range
- Splits a DataFrame into equal-sized chunks
Correct answer: Bins continuous data into discrete intervals
pd.cut() segments and sorts continuous values into bins, returning a Categorical object.
Question 6: Which NumPy operation performs element-wise multiplication of two arrays (NOT matrix multiplication)?
- np.dot(A, B)
- A @ B
- A * B (Correct answer)
- np.cross(A, B)
Correct answer: A * B
The * operator performs element-wise (Hadamard) multiplication; np.dot and @ perform matrix multiplication.
Question 7: In a pandas groupby operation, what does .agg({'col1': 'sum', 'col2': 'mean'}) do?
- Applies sum to all columns and mean to col2 only
- Applies different aggregation functions to specific columns simultaneously (Correct answer)
- Raises a ValueError because mixed aggregations are not allowed
- Returns two separate DataFrames
Correct answer: Applies different aggregation functions to specific columns simultaneously
Passing a dict to .agg() allows applying different aggregation functions to different columns in a single call.
When reading a CSV with pd.read_csv(), which parameter skips the first two rows of data?