Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame(np.arange(12).reshape(3, 4),
                  columns=['P', 'Q', 'R', 'S'])
df
Out[2]:
P Q R S
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11

Pandas: DataFrame-drop

Drop columns:

In [3]:
df.drop(['Q', 'R'], axis=1)
Out[3]:
P S
0 0 3
1 4 7
2 8 11

Pandas: DataFrame-drop - columns

In [4]:
df.drop(columns=['Q', 'R'])
Out[4]:
P S
0 0 3
1 4 7
2 8 11

Pandas: DataFrame-drop - columns

Drop a row by index:

In [5]:
df.drop([0, 1])
Out[5]:
P Q R S
2 8 9 10 11

Pandas: DataFrame - drop a row by index

Drop columns and/or rows of MultiIndex DataFrame

In [6]:
midx = pd.MultiIndex(levels=[['deer', 'dog', 'eagle'],
                             ['speed', 'weight', 'length']],
                      codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
                            [0, 1, 2, 0, 1, 2, 0, 1, 2]])
df = pd.DataFrame(index=midx, columns=['big', 'small'],
                   data=[[60, 35], [30, 12], [5.5, 1], [40, 20],
                         [25, 10], [4.5, 0.8], [320, 220],
                         [2, 0.8], [2.2, 0.2]])
df
Out[6]:
big small
deer speed 60.0 35.0
weight 30.0 12.0
length 5.5 1.0
dog speed 40.0 20.0
weight 25.0 10.0
length 4.5 0.8
eagle speed 320.0 220.0
weight 2.0 0.8
length 2.2 0.2

Pandas: DataFrame - Drop columns and/or rows of MultiIndex DataFrame

In [7]:
df.drop(index='dog', columns='small')
Out[7]:
big
deer speed 60.0
weight 30.0
length 5.5
eagle speed 320.0
weight 2.0
length 2.2

Pandas: DataFrame - Drop columns small

In [8]:
df.drop(index='length', level=1)
Out[8]:
big small
deer speed 60.0 35.0
weight 30.0 12.0
dog speed 40.0 20.0
weight 25.0 10.0
eagle speed 320.0 220.0
weight 2.0 0.8

Pandas: DataFrame - Drop index length