All sample questions / pandas

What does print(resultat, df['a'].tolist()) print? pandas Copy-on-Write, Views and Assignment

Block 7, Copy-on-Write, Views and Assignment, question 17 of the pandas sample

import pandas as pd
df = pd.DataFrame({'a': [1, 1, 2]})
resultat = df.drop_duplicates(inplace=True)
print(resultat, df['a'].tolist())

What does this print?

  1. A. None [1, 2]
  2. B. None [1, 1, 2]
  3. C. a\n0 1\n2 2 [1, 2]
  4. D. [1, 2] [1, 2]
Show the answer and why each option is right or wrong

Answer, A. None [1, 2]

A. None [1, 2]
Correct. drop_duplicates with inplace=True removes the repeated row directly from df and returns None, so resultat is None and df['a'] now reads [1, 2].
B. None [1, 1, 2]
Assumes the inplace call returns None correctly but the duplicate row was never actually removed from df.
C. a\n0 1\n2 2 [1, 2]
Assumes resultat holds the whole deduplicated DataFrame rather than None.
D. [1, 2] [1, 2]
Assumes resultat holds the deduplicated column as a list instead of None.

drop_duplicates with inplace=True follows the same convention as the other inplace methods here, mutate and return None. The duplicate is genuinely gone from df, the empty return value only reflects the call itself, not the state of df.

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.