How to delete a table element in a Pandas DataFrame. A summary of the drop method options based on actual usage.
It is complicated because there is the same processing with different descriptions, You can delete a matrix by remembering "columns = label" and "index = label".
Click here for a list of options ▶ [Drop method option list quick reference table](# 1 drop method option list quick reference table)
option | Description example | Contents |
---|---|---|
(columns=label) | df.drop(columns=['x', 'y']) | Column x,Delete y |
(index=label) | df.drop(index=['A', 'B'] | Line A,Delete B |
(labe) | df.drop(['A', 'B']) | Line A,Delete B |
(label, axis='index') | df.drop(['A', 'B'], axis='index') | Line A,Delete B |
(label, axis=1) | df.drop(['x', 'y'], axis=1) | Column x,Delete y |
(label, axis='columns') | df.drop(['x', 'y'], axis='columns') | Column x,Delete y |
(errors='ignore') | df.drop(['aaa'], axis='columns', errors='ignore') | Ignore error |
(inplace=True) | df.drop(['A', 'B'], inplace=True) | Overwrite |
--"Df": Variable with table --"Label": row or column name --Create another object without overwriting the default --Matrix can be specified only with either "index =" and "columns =" or "axis = 1".
Table code
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
listB = ['EEE', 'FFF', 'GGG', 'HHH']
listC = ['III', 'JJJ', 'KKK', 'LLL']
listD = ['MMM', 'NNN', 'OOO', 'PPP']
df = pd.DataFrame(listA)
df[1] = listB
df[2] = listC
df[3] = listD
df.columns = ['aaa', 'bbb', 'ccc', 'ddd']
df.index = [111,222,333,444]
df
①drop(label, axis) ②drop(index=label) ③drop(columns=label)
** Basic type **
df.drop(label, axis=A)
└ "df": Table data (DataFrame type)
└ "drop ()": Drop method
└ "label": Row / column name to delete
└ "axis = A": Specify row or column with A
** ▼ Point ** --Specify either row or column with axis. --There are 4 patterns for specifying axsi. --A new DataFrame object is created with the original table intact. --Can be restored even if erased by mistake
** ▼ How to specify 4 axes **
value | Description example | Contents |
---|---|---|
0 | axis=0(Optional) | 行名で指定。デフォルト。Optional |
1 | axis=1 | Specified by column name |
index | axis='index' | Specified by line name |
columns | axis='columns' | Specified by column name |
The following two axes (three including omissions) can be described.
① axis = 0 (optional) ②axis='index'
(1-1) axis=0
df.drop([label1,label2,,,], axis=0)
df.drop([label1,label2,,,])
Even if axis = 0 is ** omitted **, the same processing will be performed.
** ▼ Specify only one column **
(axis=0) Omitted
df.drop(333)
#output
aaa bbb ccc ddd
111 AAA EEE III MMM
222 BBB FFF JJJ NNN
444 DDD HHH LLL PPP
(axis=0) No omission
df.drop(333, axis=0)
#output
aaa bbb ccc ddd
111 AAA EEE III MMM
222 BBB FFF JJJ NNN
444 DDD HHH LLL PPP
Multiple column names
df.drop([111,333])
#output
aaa bbb ccc ddd
222 BBB FFF JJJ NNN
444 DDD HHH LLL PPP
df.drop([label1,label2,,,], axis='index')
axis='index'
df.drop([111,333], axis='index')
#output
aaa bbb ccc ddd
222 BBB FFF JJJ NNN
444 DDD HHH LLL PPP
①axis=1 ②axis='columns'
(1-3) axis=1
df.drop([label1,label2,,,], axis=1)
└ “1” of axis = 1 points to the row of column name.
** ▼ Specify only one column ** In the case of one column, the same processing is performed even if there is no [].
df.drop(['AAA'], axis=1)
df.drop('AAA', axis=1)
(axis=1)
df.drop('bbb', axis=1)
#output
aaa ccc ddd
111 AAA III MMM
222 BBB JJJ NNN
333 CCC KKK OOO
444 DDD LLL PPP
Multiple column names
df.drop(['bbb','ddd'], axis=1)
#output
aaa ccc
111 AAA III
222 BBB JJJ
333 CCC KKK
444 DDD LLL
df.drop([label1,label2,,,], axis='columns')
axis='columns'
df.drop(['bbb','ddd'], axis='columns')
#output
aaa ccc
111 AAA III
222 BBB JJJ
333 CCC KKK
444 DDD LLL
df.drop(index=label)
└ Describe the line name you want to delete in "label".
index=label
df.drop(index=[222,444])
#output
aaa bbb ccc ddd
111 AAA EEE III MMM
333 CCC GGG KKK OOO
The process is the same as the following three.
df.drop(label)
df.drop(label, axis=0)
df.drop(label, axis='index')
df.drop(columns=label)
└ "label": Describe the column name you want to delete
index=label
df.drop(columns=['ddd','bbb'])
#output
aaa ccc
111 AAA III
222 BBB JJJ
333 CCC KKK
444 DDD LLL
Same as the following two.
df.drop(label, axis=1)
df.drop(label, axis='columns')
The default does not change the original table and creates a new object. (It can be restored even if it is deleted by mistake)
Option to turn off this feature.
inplace=True
└ Default is "False"
Allow overwrite
df.drop(columns=['ddd','bbb'], inplace=True)
df
#output
aaa ccc
111 AAA III
222 BBB JJJ
333 CCC KKK
444 DDD LLL
The original table (df) has been rewritten.
Delete non-existent column
df.drop(columns=['xxx'], errors='ignore')
Nothing is displayed.
If there is no description of "errors ='ignore'", it will be displayed as follows. ⇒「KeyError: "['xxx'] not found in axis"」