Data Science with Python Certification Data Analysis with Python 5 — Questions and Answers
Question 1: Which pandas method efficiently applies a function along an axis of a DataFrame without using a Python loop?
- df.map(func)
- df.apply(func, axis=1) (Correct answer)
- df.run(func)
- df.execute(func)
Correct answer: df.apply(func, axis=1)
df.apply() applies a function along rows (axis=1) or columns (axis=0), leveraging pandas internals for efficiency.
Question 2: What is the effect of setting inplace=True in df.drop(columns=['col'], inplace=True)?
- Returns a new DataFrame without the column
- Modifies the original DataFrame and returns None (Correct answer)
- Creates a deep copy without the column
- Raises a DeprecationWarning in pandas 2.x
Correct answer: Modifies the original DataFrame and returns None
inplace=True mutates the DataFrame directly and returns None instead of a new object.
Question 3: Which NumPy function computes the cumulative sum of array elements along a given axis?
- np.sum(arr, axis=0)
- np.cumsum(arr) (Correct answer)
- np.running_sum(arr)
- np.add.accumulate is only for 1D arrays
Correct answer: np.cumsum(arr)
np.cumsum() returns an array of the same shape where each element is the sum of all preceding elements along the axis.
Question 4: In pandas, what does the .resample('M').sum() operation require and what does it produce?
- A numeric index; monthly totals
- A DatetimeIndex; monthly totals (Correct answer)
- Any index type; monthly means
- A MultiIndex; monthly pivot table
Correct answer: A DatetimeIndex; monthly totals
resample() requires a DatetimeIndex and resamples data to a new time frequency; 'M' groups by calendar month, summing each group.
Question 5: What is the primary difference between pd.merge() and pd.concat()?
- merge() works only on rows; concat() works only on columns
- merge() joins on key columns (SQL-style); concat() stacks DataFrames along an axis (Correct answer)
- merge() is faster than concat() for large datasets
- concat() performs an outer join by default; merge() does not
Correct answer: merge() joins on key columns (SQL-style); concat() stacks DataFrames along an axis
pd.merge() aligns rows by common key columns, while pd.concat() simply stacks DataFrames without key-based alignment.
Question 6: When using np.random.seed(42) before generating random data, what is achieved?
- The random numbers are cryptographically secure
- Results are reproducible across runs with the same seed (Correct answer)
- The distribution is always normal regardless of the function used
- Only 42 random numbers can be generated in that session
Correct answer: Results are reproducible across runs with the same seed
Setting a seed initializes the pseudo-random number generator to a fixed state, guaranteeing the same sequence of numbers each run.
Question 7: Which pandas method provides a quick statistical summary including count, mean, std, min, and quartiles?
- df.info()
- df.summary()
- df.describe() (Correct answer)
- df.stats()
Correct answer: df.describe()
df.describe() generates descriptive statistics for numeric columns, including count, mean, std, and percentile values.
Which pandas method efficiently applies a function along an axis of a DataFrame without using a Python loop?