Q.In a Pandas DataFrame, which value for axis will be used to delete a column ? (A) axis = -1 (B) axis = 0 (C) axis = 1 (D) axis = 2
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →In Pandas, axis=1 refers to columns (the horizontal axis), so deleting a column requires axis=1. The answer is (C).
Understanding the axis parameter in Pandas
The axis parameter in Pandas tells operations which dimension of the DataFrame to work along. Think of a DataFrame as a two-dimensional table: it has rows stacked vertically and columns arranged horizontally.
Pandas borrowed NumPy's convention for labeling these dimensions:
axis=0runs down the rows (the vertical direction). Operations withaxis=0work across rows, meaning they affect or aggregate entire columns.axis=1runs across the columns (the horizontal direction). Operations withaxis=1work along rows, meaning they affect or aggregate entire rows—or in the case of deletion, remove entire columns.
The naming can feel counterintuitive at first. When you specify axis=1 for deletion, you're saying "operate along the column axis," which means you're selecting columns to remove.
Deleting a column: the mechanics
When you call df.drop() to remove a column, you must specify axis=1:
df.drop('column_name', axis=1)
Without axis=1, Pandas defaults to axis=0 and tries to drop a row with that label, which will raise an error if no such row exists. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.