How do you drop multiple columns in Pandas?

In Pandas, you can drop multiple columns by specifying the list of column names in the drop() function. You can also use the axis argument to specify whether you want to drop rows or columns. The axis=1 argument indicates that you want to drop columns, while axis=0 indicates that you want to drop rows.


You can use the following methods to drop multiple columns from a pandas DataFrame:

Method 1: Drop Multiple Columns by Name

df.drop(columns=['col1', 'col2', 'col4'], inplace=True)

Method 2: Drop Columns in Range by Name

df.drop(columns=df.loc[:, 'col1':'col4'], inplace=True)

Method 3: Drop Multiple Columns by Index

df.drop(columns=df.columns[[0, 3, 4]], inplace=True)

Method 4: Drop Columns in Range by Index

df.drop(columns=df.columns[1:4], inplace=True)

Note: The argument inplace=True tells pandas to drop the columns in place without reassigning the DataFrame.

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12],
                   'steals': [4, 5, 10, 12, 4, 8, 7, 2]})

#view DataFrame
print(df)

  team  points  assists  rebounds  steals
0    A      18        5        11       4
1    B      22        7         8       5
2    C      19        7        10      10
3    D      14        9         6      12
4    E      14       12         6       4
5    F      11        9         5       8
6    G      20        9         9       7
7    H      28        4        12       2

Example 1: Drop Multiple Columns by Name

The following code shows how to drop the points, rebounds, and steals columns by name:

#drop multiple columns by name
df.drop(columns=['points', 'rebounds', 'steals'], inplace=True)

#view updated Dataframe
print(df)

  team  assists
0    A        5
1    B        7
2    C        7
3    D        9
4    E       12
5    F        9
6    G        9
7    H        4

Example 2: Drop Columns in Range by Name

The following code shows how to drop each column between the points and rebounds columns by name:

#drop columns in range by name
df.drop(columns=df.loc[:, 'points':'rebounds'], inplace=True)

#view updated Dataframe
print(df)

  team  steals
0    A       4
1    B       5
2    C      10
3    D      12
4    E       4
5    F       8
6    G       7
7    H       2

Example 3: Drop Multiple Columns by Index

The following code shows how to drop the columns in index positions 0, 3 and 4 from the DataFrame:

#drop multiple columns by index
df.drop(columns=df.columns[[0, 3, 4]], inplace=True)

#view updated Dataframe
print(df)

   points  assists
0      18        5
1      22        7
2      19        7
3      14        9
4      14       12
5      11        9
6      20        9
7      28        4

Example 4: Drop Columns in Range by Index

The following code shows how to drop the columns in index positions 0, 3 and 4 from the DataFrame:

#drop columns by index range
df.drop(columns=df.columns[1:4], inplace=True)

#view updated Dataframe
print(df)

  team  steals
0    A       4
1    B       5
2    C      10
3    D      12
4    E       4
5    F       8
6    G       7
7    H       2

Note that the syntax df.columns[1:4] specifies columns in index positions 1 up to 4.

Thus, this syntax drops the columns in index positions 1, 2 and 3.

Note: You can find the complete documentation for the pandas drop() function .

x