What is the command to drop a column in a DataFrame 'df'?
What is the command to drop a column in a DataFrame 'df'?
Solution
✅ Correct Option: 2
In pandas, 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.
Related questions:
2025: 20 May Shift 1
2025: 27 May Shift 2