Skip to main contentSkip to solution

What is the command to drop a column in a DataFrame 'df'?

Solution

✅ Correct Option: 2

In pandas, axis=1axis = 1 refers to columns, so df.drop('col_name', axis = 1) removes the column named col_name. It returns a new DataFrame unless inplace = True is passed.


Option 1: axis = 0 refers to rows (index labels), so it would try to drop a row labelled col_name.

Options 3 and 4: drop is a method, so it must be called with round brackets (), not square brackets []. A DataFrame has only axes 0 and 1, so axis = 2 or axis = 3 is invalid.

Memory trick: axis 0 runs down the rows, axis 1 runs across the columns.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question