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)
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
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'.
Related questions:
2026: 29 May Shift 2
2026: 21 May Shift 1
2026: 18 May Shift 2