Consider the following table:
Priya Richa Rohit Soham Riya Maths 85 87 84 76 90 Science 86 76 86 66 92 Hindi 92 91 83 68 97 English 90 81 90 75 94
How to delete columns having the labels 'Rohit' and 'Soham' if the dataframe is represented using 'df' ?
Consider the following table:
| Priya | Richa | Rohit | Soham | Riya | |
|---|---|---|---|---|---|
| Maths | 85 | 87 | 84 | 76 | 90 |
| Science | 86 | 76 | 86 | 66 | 92 |
| Hindi | 92 | 91 | 83 | 68 | 97 |
| English | 90 | 81 | 90 | 75 | 94 |
How to delete columns having the labels 'Rohit' and 'Soham' if the dataframe is represented using 'df' ?
Solution
The pandas method for removing labels is drop(). Columns are removed with axis = 1, so the intended call is df.drop(['Rohit', 'Soham'], axis = 1). Option 4 is the only one that uses drop with axis = 1.
The closing bracket after 'Soham' is missing in the printed option, which is a typo. The intended statement is the one above.
Option 2: axis = 0 looks for 'Rohit' and 'Soham' among the row labels, so it would raise a KeyError.
Options 1 and 3: del() and delete() are not DataFrame methods. Option 3 also has a bracket typo.
Remember that axis = 0 means rows and axis = 1 means columns.
Related questions:
2025: 13 May Shift 2
2025: 14 May Shift 1
2026: 23 May Shift 2