Data Science with Python Certification Introduction to Python for Data Science 5 — Questions and Answers
Question 1: What does `df.groupby('category').mean()` compute in pandas?
- The mean of the 'category' column only
- The mean of all numeric columns grouped by unique values in 'category' (Correct answer)
- The most frequent value in each category group
- The total count of rows in each category
Correct answer: The mean of all numeric columns grouped by unique values in 'category'
`groupby` splits the DataFrame by unique values in 'category' and `.mean()` computes the column-wise mean for each group.
Question 2: What is the output of `list(map(lambda x: x*2, [1, 2, 3]))`?
- [1, 2, 3]
- [2, 4, 6] (Correct answer)
- [1, 4, 9]
- [2, 3, 4]
Correct answer: [2, 4, 6]
`map` applies the lambda function (doubling each element) to every item in the list, producing [2, 4, 6].
Question 3: Which seaborn function is best suited for visualizing the distribution of a single continuous variable?
- sns.scatterplot()
- sns.histplot() (Correct answer)
- sns.barplot()
- sns.lineplot()
Correct answer: sns.histplot()
`sns.histplot()` displays the distribution of a single variable using histogram bins, optionally with a KDE overlay.
Question 4: What is the purpose of `np.reshape()` in NumPy?
- Changes the data type of an array
- Returns a new array with the same data but a different shape (Correct answer)
- Sorts the elements of an array
- Transposes a matrix
Correct answer: Returns a new array with the same data but a different shape
`np.reshape(array, new_shape)` returns a view or copy of the array rearranged into the specified shape without altering data.
Question 5: In Python, what is the difference between `deepcopy` and a shallow copy of an object?
- Shallow copy duplicates nested objects; deepcopy does not
- Deepcopy creates independent copies of nested objects; shallow copy only copies the top-level container (Correct answer)
- They are functionally identical for all data types
- Deepcopy only works with NumPy arrays
Correct answer: Deepcopy creates independent copies of nested objects; shallow copy only copies the top-level container
A shallow copy copies the container but references the same nested objects; `deepcopy` recursively copies all nested objects as well.
Question 6: Which Python built-in function returns the index and value of each item when iterating over a list?
- zip()
- map()
- enumerate() (Correct answer)
- index()
Correct answer: enumerate()
`enumerate(iterable)` yields `(index, value)` pairs, making it easy to track position while iterating.
Question 7: What does the `axis` parameter control in pandas aggregation methods like `df.sum(axis=1)`?
- Sorts the result along that axis
- Applies the aggregation across columns (row-wise) when axis=1 (Correct answer)
- Selects a single column by position
- Sets the index to the specified axis number
Correct answer: Applies the aggregation across columns (row-wise) when axis=1
`axis=0` aggregates down rows (column-wise totals); `axis=1` aggregates across columns (row-wise totals).
What does `df.groupby('category').mean()` compute in pandas?