All sample questions / pandas

What does print(type(combined).__name__, combined.tolist()) print? pandas Merge, Join and Concat

Block 5, Merge, Join and Concat, question 13 of the pandas sample

import pandas as pd
s1 = pd.Series([1, 2], name='s')
s2 = pd.Series([3, 4], name='s')
combined = pd.concat([s1, s2])
print(type(combined).__name__, combined.tolist())

What does this print?

  1. A. list [1, 2, 3, 4]
  2. B. Series [1, 2]
  3. C. DataFrame [1, 2, 3, 4]
  4. D. Series [1, 2, 3, 4]
Show the answer and why each option is right or wrong

Answer, D. Series [1, 2, 3, 4]

A. list [1, 2, 3, 4]
Assumes the result loses its pandas Series identity and becomes a plain Python list.
B. Series [1, 2]
Assumes only the second Series survives the concatenation instead of both being stacked together.
C. DataFrame [1, 2, 3, 4]
Assumes stacking two Series with concat upgrades the result to a DataFrame the way combining them with a dict of columns would.
D. Series [1, 2, 3, 4]
Correct. Concatenating two Series with concat keeps the result as a single Series, stacking their values in order, so the type name prints as Series and the combined values are 1, 2, 3, 4.

concat inspects what it is given and keeps the same container kind for the result, a list of Series concatenates into one longer Series, just as a list of DataFrames concatenates into one DataFrame. Both inputs sharing the same name here has no effect on this, the values are simply stacked end to end in the order the inputs were listed.

The answer above is what pandas 3.0.5 printed when the snippet was run, not what someone expected it to do.

This question comes from a bank of 255 pandas interview questions, all built the same way, sold as one PDF with the full answer key.

Get the 255 pandas questions, 9 euros

Or SQL, Python, pandas and NumPy together, 1,636 questions, 19 euros

Or play the free pandas sample, twenty questions in the browser, also as a free PDF.

Previous question Next question

Did this save you a lookup? Buy me a coffee on Ko-fi.