Use the drop() method to drop specified labels from rows or columns in a DataFrame. The method takes a single label or a list of labels as an argument and returns a new DataFrame with the specified rows or columns removed.
Arguments:
- labels
This is an optional argument. It could be single label or list-like. It also can be Index or column labels to drop depending on the axis argument.
- axisThis is an optional argument. Use the axis argument to specify whether the labels refer to index labels or column labels:
- 0 or 'index' for index labels
- 1 or 'column' for column labels
- columns
This is an optional argument. Use the columns argument to specify a single column label or a list of column labels. The columns argument is an alternative to specifying axis=1 with labels. You cannot specify both labels and columns.
Examples Prerequisite
Assume the table "admissions_train" exists and its index column is "id". And a DataFrame "df" is created based on this table using the command:
>>> df = DataFrame("admissions_train")
>>> df masters gpa stats programming admitted id 5 no 3.44 novice novice 0 7 yes 2.33 novice novice 1 22 yes 3.46 novice beginner 0 17 no 3.83 advanced advanced 1 13 no 4.00 advanced novice 1 19 yes 1.98 advanced advanced 0 36 no 3.00 advanced novice 0 15 yes 4.00 advanced advanced 1 34 yes 3.85 advanced beginner 0 40 yes 3.95 novice beginner 0
Example 1: Drop columns 'stats' and 'admitted' with axis=1
>>> df.drop(['stats', 'admitted'], axis=1)
programming masters gpa id 5 novice no 3.44 34 beginner yes 3.85 13 novice no 4.00 40 beginner yes 3.95 22 beginner yes 3.46 19 advanced yes 1.98 36 novice no 3.00 15 advanced yes 4.00 7 novice yes 2.33 17 advanced no 3.83
Example 2: Drop columns 'stats' and 'admitted' with columns argument
>>> df.drop(columns=['stats', 'admitted'])
programming masters gpa id 5 novice no 3.44 34 beginner yes 3.85 13 novice no 4.00 19 advanced yes 1.98 15 advanced yes 4.00 40 beginner yes 3.95 7 novice yes 2.33 22 beginner yes 3.46 36 novice no 3.00 17 advanced no 3.83
Example 3: Drop rows with index values 13 and 15 with axis=0
>>> df1 = df[df.gpa == 4.00] >>> df1 masters gpa stats programming admitted id 13 no 4.0 Advanced Novice 1 29 yes 4.0 Novice Beginner 0 15 yes 4.0 Advanced Advanced 1
>>> df1.drop([13,15], axis=0)
masters gpa stats programming admitted id 29 yes 4.0 Novice Beginner 0 >>>