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

Pandas MCQs · Page 2

83 multiple choice questions with answers in Data Science.

Showing 26–50 of 83

A wide table with one column per month must become long, with month and value columns. Which call does that?

  1. df.transpose, which swaps rows and columns and therefore creates a long form table
  2. df.melt with id_vars, which turns the monthly columns into rows of key-value pairs Correct answer
  3. df.pivot with the month columns as index, which expands the columns into extra rows
  4. df.stack on axis=1, which returns a wide frame with the months kept as columns
MCQSEXAM Read explanation

df.pivot_table(index='dept', columns='month', values='sales') is called without aggfunc. What happens when several rows share a dept and month?

  1. The duplicate cells are averaged, because the default aggregation is the mean Correct answer
  2. A ValueError is raised, because duplicate entries need an explicit aggfunc
  3. The last row for each cell wins, because pivot_table keeps the final value
  4. The rows are concatenated into a list inside the cell, preserving every value
MCQSEXAM Read explanation

pd.concat([a, b], axis=1) is used on two frames whose indexes overlap only partially. What is the default result?

  1. Only labels present in both frames are kept, as an inner join on the index
  2. The frames are concatenated positionally and the index is ignored, so no alignment occurs
  3. The index union is used and missing labels produce NaN, as an outer join on the index Correct answer
  4. An error is raised because the indexes are not identical, and the frames must be reindexed
MCQSEXAM Read explanation

What caveat applies to df.copy(deep=True) for a column of dtype object holding Python lists?

  1. The data and index are copied, but the Python objects inside object columns are not recursively copied, so they remain shared Correct answer
  2. deep=True is not allowed for object dtype columns, because pandas cannot deep-copy arbitrary Python objects
  3. Every nested list is deep-copied, guaranteeing full independence, so mutating one frame never affects the contents of the other
  4. deep=True copies only the index and shares all of the column data, while deep=False copies the column arrays
MCQSEXAM Read explanation

What does converting a low-cardinality object column with astype('category') change?

  1. It converts every string to an interned string object, for faster hashing and lower memory use
  2. It stores integer codes plus a dictionary of categories, cutting memory and enabling ordered comparisons Correct answer
  3. It drops duplicate values from the column, keeping only the first occurrence of every distinct string value
  4. It permanently sorts the DataFrame by that column and rebuilds the row index in sorted order
MCQSEXAM Read explanation

What does pd.merge(left, right, how='outer', indicator=True) add to the result?

  1. A boolean column showing whether the join key was unique in both of the input frames
  2. It raises an error if any of the keys fail to match, instead of reporting them in the result
  3. It sorts the result by the join key automatically and resets the index of the output
  4. A _merge column whose values are left_only, right_only or both, showing each row's origin Correct answer
MCQSEXAM Read explanation

How does df.groupby('k')['x'].transform('mean') differ from .apply('mean')?

  1. Both return one row per group, differing only in the index name that the resulting object carries afterwards
  2. transform can only be used with user-defined functions, while apply also accepts built-in method strings
  3. transform returns a Series aligned to the original index with the same length, while apply returns one value per group Correct answer
  4. apply returns a column the same length as the input frame itself, while transform reduces the result to one row per group
MCQSEXAM Read explanation

df2 = df[df.a > 0]; df2['b'] = 1; raises SettingWithCopyWarning. Why?

  1. Because boolean indexing always returns a tuple of arrays, so assignment cannot tell which of the two arrays to modify in place here
  2. Because column b does not exist in df, so pandas must decide whether to create it on the copy or on the parent frame
  3. Boolean indexing returns a new object that may still reference the parent's data, so chained assignment has an ambiguous target Correct answer
  4. Because assigning a scalar to a new column is not allowed after filtering, and pandas raises a warning instead
MCQSEXAM Read explanation