1. Home
  2. Subjects
  3. Data Science
  4. Pandas

Pandas MCQs

83 multiple choice questions with answers in Data Science.

Test your knowledge with Pandas MCQs covering DataFrames and Series, indexing, filtering, grouping, merging and cleaning data. Use them for daily revision and to find the topics you still need to study. Helpful for NTS, PPSC IT tests and campus interviews.

Showing 1–25 of 83

What does pd.concat([a, b], keys=['x','y']) add to the result?

  1. Columns prefixed with x_ and y_, aligned on the shared index labels
  2. A dictionary of the two frames keyed by x and y instead of a frame
  3. A MultiIndex whose outer level records which frame each row came from Correct answer
  4. A new column named keys holding x or y for each contributing row of both
MCQSEXAM Read explanation

A pattern capturing one or more digits is applied with Series.str.extract to a column where some values do not match. What appears in those rows?

  1. NaN in the extracted column, because a non-match yields a missing value Correct answer
  2. An empty string, because extract returns the unmatched part of the input
  3. The original value unchanged, since extract only rewrites matching rows
  4. An error, because extract requires every value to match the pattern
MCQSEXAM Read explanation

df.query('price > @limit') is evaluated inside a function that holds limit as a local variable. Why does this work?

  1. The @ prefix escapes the name, allowing identifiers that are not valid Python
  2. The @ prefix resolves the name in the caller's scope instead of as a column label Correct answer
  3. The @ prefix marks the referenced name as a pandas Series, so query can broadcast it
  4. The @ prefix disables parsing of the expression, passing it to Python's eval directly
MCQSEXAM Read explanation

What does groupby(...).apply(f) do with the grouping columns when f returns a DataFrame in pandas 2.x?

  1. It drops the grouping columns entirely, since apply only ever sees the remaining value columns
  2. It raises an error, because apply callbacks returning DataFrames are no longer supported
  3. It duplicates the keys as columns and as index levels, producing redundant result fields
  4. It prepends the group keys to the result index, so include_groups=False changes the output Correct answer
MCQSEXAM Read explanation

Under the copy-on-write semantics of pandas 2.x, df[df.a > 0]['b'] = 1 is executed. What is the effect?

  1. The whole column b is set to 1 for every row, since the mask is ignored here
  2. Nothing changes in df, because the chained assignment writes to a temporary copy Correct answer
  3. Column b is updated for the matching rows, exactly as an equivalent .loc assignment would
  4. A SettingWithCopyWarning is raised and the assignment is then silently applied
MCQSEXAM Read explanation

df.drop_duplicates(subset=['email'], keep='last') is applied to a frame with repeated emails. What is retained?

  1. The last row of the frame only, because keep applies to the whole frame rather than rows
  2. Both the first and last occurrences, since keep='last' keeps the boundary rows intact
  3. The last occurrence of each email in the current row order, dropping the earlier ones Correct answer
  4. The first occurrence of each email address, because keep names the copy inspected
MCQSEXAM Read explanation

A column holds integers and NaN. Why does astype('int64') fail, and what is the usual fix?

  1. The cast truncates NaN to zero, raising a warning rather than an error
  2. The fix is to cast to float16 first, because float16 supports missing values natively
  3. int64 has no NaN representation, so the nullable Int64 dtype must be used instead Correct answer
  4. int64 holds NaN perfectly well, but the cast is refused because of an unrelated mismatch
MCQSEXAM Read explanation

df.resample('M').sum() is called on a DataFrame whose date lives in an ordinary string column rather than the index. What happens?

  1. The call groups by calendar month names, treating the strings as categorical data
  2. An empty frame is returned, because no rows fall on the month-end timestamps
  3. A TypeError about the index, because resample needs a DatetimeIndex on the axis Correct answer
  4. The string column is parsed automatically and the monthly totals are returned
MCQSEXAM Read explanation

A categorical column with 20 categories is grouped, but only 3 categories occur in the data. Why does observed=True shrink the result?

  1. Because observed=True drops rows whose category does not appear among the group keys chosen
  2. Because grouping a categorical converts it to an object column, losing category metadata
  3. Because observed=True sorts the categories and then truncates the output index
  4. Because the default expands every category combination, producing empty groups too Correct answer
MCQSEXAM Read explanation

A skewed income column must be split into four groups of equal count. Which function does that, and what does the other do instead?

  1. qcut, which uses sample quantiles, whereas cut splits on equal-width value ranges Correct answer
  2. cut, which uses sample quantiles, whereas qcut splits on equal-width value ranges
  3. cut, because both functions are identical except for the naming of their labels
  4. qcut, because it always produces equal-width bins regardless of the data distribution
MCQSEXAM Read explanation

df['city'].isin(['Lahore', 'Karachi']) is used as a row filter. What does it return, and how are NaN rows treated?

  1. A boolean Series aligned to the index, with NaN rows marked False unless NaN is listed Correct answer
  2. A boolean Series of identical length, with NaN rows marked True so every record is kept
  3. An integer array of row positions, because isin returns the matching row numbers
  4. A filtered DataFrame directly, since isin applies the mask as soon as it is called
MCQSEXAM Read explanation

Teacher or blogger? Put this quiz on your website

Free Pandas MCQs quiz with answers for your school site, academy page or blog. Copy one line of code – works on WordPress and Blogger.

Get the quiz widget

All 83 Pandas questions

Show the full list of questions in this topic