- Home
- Subjects
- Data Science
- Pandas
Pandas MCQs · Page 2
83 multiple choice questions with answers in Data Science.
-
A
df.transpose, which swaps rows and columns and therefore creates a long form table
-
B
df.melt with id_vars, which turns the monthly columns into rows of key-value pairs
Correct answer
-
C
df.pivot with the month columns as index, which expands the columns into extra rows
-
D
df.stack on axis=1, which returns a wide frame with the months kept as columns
-
A
Scattered in place, because pandas leaves NaN rows where they originally stood
-
B
The call raises, because sorting a column with NaN requires dropna first
-
C
At the end by default, because na_position defaults to last in sort_values
Correct answer
-
D
At the beginning by default, because NaN sorts before every numeric value
-
A
The duplicate cells are averaged, because the default aggregation is the mean
Correct answer
-
B
A ValueError is raised, because duplicate entries need an explicit aggfunc
-
C
The last row for each cell wins, because pivot_table keeps the final value
-
D
The rows are concatenated into a list inside the cell, preserving every value
-
A
An error, because the series is shorter than the window
-
B
NaN, because min_periods defaults to the window size
Correct answer
-
C
The mean of the available values, because partial windows are used
-
D
Zero, because missing window values are treated as zero
-
A
To skip that column entirely while reading, so it never appears in the frame
-
B
To speed up parsing by disabling the C engine and using the Python parser
-
C
To stop pandas inferring ZIP codes as integers and stripping leading zeros
Correct answer
-
D
To force every column in the file to be parsed as text rather than numbers
-
A
Relative frequencies excluding NaN, so the resulting proportions sum to less than one overall
-
B
Boolean flags indicating whether each value is missing, aligned to the original index
-
C
Absolute counts per value, excluding NaN, sorted in ascending order of frequency
-
D
Relative frequencies per distinct value, including NaN as its own category, summing to 1
Correct answer
-
A
Only labels present in both frames are kept, as an inner join on the index
-
B
The frames are concatenated positionally and the index is ignored, so no alignment occurs
-
C
The index union is used and missing labels produce NaN, as an outer join on the index
Correct answer
-
D
An error is raised because the indexes are not identical, and the frames must be reindexed
-
A
A NumPy array of the whole frame
-
B
A tuple of the row values in column order
-
C
A Series for each row, indexed by the column labels
Correct answer
-
D
A Series for each column, indexed by the row labels
-
A
The data and index are copied, but the Python objects inside object columns are not recursively copied, so they remain shared
Correct answer
-
B
deep=True is not allowed for object dtype columns, because pandas cannot deep-copy arbitrary Python objects
-
C
Every nested list is deep-copied, guaranteeing full independence, so mutating one frame never affects the contents of the other
-
D
deep=True copies only the index and shares all of the column data, while deep=False copies the column arrays
-
A
Columns containing at least three non-missing values
-
B
Rows containing at least three non-missing values
Correct answer
-
C
Rows containing fewer than three missing values
-
D
Rows containing exactly three non-missing values
-
A
res.droplevel('year') returns a Series indexed by month only
Correct answer
-
B
res.reset_index(level='year') removes the level without adding a column
-
C
res.drop('year') drops the level from the index
-
D
res.swaplevel() removes the level entirely
-
A
It converts the bad entries to zero
-
B
It replaces them with NaN and returns a numeric dtype column
Correct answer
-
C
It leaves the column as object and keeps the original strings
-
D
It raises a ValueError listing the offending values
-
A
It converts every string to an interned string object, for faster hashing and lower memory use
-
B
It stores integer codes plus a dictionary of categories, cutting memory and enabling ordered comparisons
Correct answer
-
C
It drops duplicate values from the column, keeping only the first occurrence of every distinct string value
-
D
It permanently sorts the DataFrame by that column and rebuilds the row index in sorted order
-
A
A boolean column showing whether the join key was unique in both of the input frames
-
B
It raises an error if any of the keys fail to match, instead of reporting them in the result
-
C
It sorts the result by the join key automatically and resets the index of the output
-
D
A _merge column whose values are left_only, right_only or both, showing each row's origin
Correct answer
-
A
Both return one row per group, differing only in the index name that the resulting object carries afterwards
-
B
transform can only be used with user-defined functions, while apply also accepts built-in method strings
-
C
transform returns a Series aligned to the original index with the same length, while apply returns one value per group
Correct answer
-
D
apply returns a column the same length as the input frame itself, while transform reduces the result to one row per group
-
A
loc returns rows 0, 1 and 2 inclusive, while iloc returns rows 0 and 1 only
Correct answer
-
B
Both return rows 0, 1 and 2
-
C
loc returns rows 0 and 1, while iloc returns 0, 1 and 2
-
D
Both return rows 0 and 1, because slices always exclude the endpoint
-
A
Because boolean indexing always returns a tuple of arrays, so assignment cannot tell which of the two arrays to modify in place here
-
B
Because column b does not exist in df, so pandas must decide whether to create it on the copy or on the parent frame
-
C
Boolean indexing returns a new object that may still reference the parent's data, so chained assignment has an ambiguous target
Correct answer
-
D
Because assigning a scalar to a new column is not allowed after filtering, and pandas raises a warning instead
-
A
agg returns a Series while transform returns a scalar
-
B
transform only works on datetime columns
-
C
transform broadcasts group results back to every original row
Correct answer
-
D
transform is faster because it skips the grouping step entirely
-
A
A long-format frame holding exactly one row for every original observation
-
B
A copy of the DataFrame sorted by its index
-
C
A frame containing only the numeric columns
-
D
A cross-tabulation with aggregated values at row and column intersections
Correct answer
-
A
The unique values without any counts attached
-
B
The number of NaN entries in the column only
-
C
Counts per distinct value, sorted most frequent first
Correct answer
-
D
Counts per distinct value, sorted in alphabetical order
-
A
Sorting a DataFrame with sort_values in place
-
B
Assigning through a chained index such as df[df.a > 0]["b"]
Correct answer
-
C
Calling dropna on a DataFrame that holds NaN values
-
D
Reading a column with df["a"] and printing its dtype attribute
-
A
Both are identical when applied to any DataFrame or Series object
-
B
map only accepts NumPy ufuncs, never functions
-
C
map transforms each element; apply works row or column wise
Correct answer
-
D
map works row wise; apply transforms each element
-
A
concat removes duplicate rows while merging keeps them
-
B
merge joins on shared keys; concat stacks along an axis
Correct answer
-
C
merge stacks along an axis; concat joins on shared keys
-
D
merge only works on datetime indexes
-
A
Sales summed across all columns grouped by city
-
B
Total sales per distinct city value
Correct answer
-
C
Overall sales total ignoring the city column
-
D
The row count in each city group
-
A
Both address the row whose label equals 2
-
B
loc works only on columns, iloc only on rows
-
C
loc uses the index label 2; iloc uses position 2
Correct answer
-
D
loc uses position 2; iloc uses the index label 2