Skip to main contentSkip to solution

Which of the following are the correct commands to delete a column from the DataFrame 'df1'?

(A) df1=df1.drop(column name,axis=1)

(B) df1.drop(column name,axis=columns,inplace=True)

(C) df1.drop(column name,axis='columns',inplace=True)

(D) df1.drop(column name,axis=1,inplace=True)

Solution

✅ Correct Option: 1

DataFrame.drop() removes a column when axis=1 or axis='columns' is given. Without inplace=True, it returns a new DataFrame, so the result must be assigned back, as in (A). With inplace=True, the DataFrame is changed directly and nothing needs to be assigned, as in (C) and (D).

(A), (C) and (D) are therefore valid.


(B): axis=columns has no quotes, so Python reads columns as an undefined variable name and raises a NameError. The string must be written as 'columns'.

Keyboard Shortcuts

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