drop() Method

Teradata® Python Package User Guide

brand
Teradata Vantage
prodname
Teradata Python Package
vrm_release
16.20
category
User Guide
featnum
B700-4006-098K

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.

Use the axis argument to specify whether the labels refer to index labels or column labels—0 for index label and 1 for column label. The default is 0 (index label).

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="col1".

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: Drop rows with index values 34 and 13 with axis=0

>>> df.drop([34, 13], axis=0)
    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
19      yes  1.98  advanced    advanced        0
15      yes  4.00  advanced    advanced        1
17       no  3.83  advanced    advanced        1
32      yes  3.46  advanced    beginner        0
11       no  3.13  advanced    advanced        1
36       no  3.00  advanced      novice        0
40      yes  3.95    novice    beginner        0

Example: 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: 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