How can I drop columns by their index in Pandas?

How can I drop columns by their index in Pandas?

Pandas is a popular Python library used for data analysis and manipulation. One of its key functionalities is the ability to drop columns from a dataset based on their index. This can be achieved by using the “.drop” method and specifying the index labels of the columns to be dropped. This process allows for efficient and precise data manipulation, making it a useful tool for managing large datasets. Additionally, Pandas offers various options for selecting and dropping columns, providing flexibility and versatility to the user.

Drop Columns by Index in Pandas


You can use the following syntax to drop one column from a pandas DataFrame by index number:

#drop first column from DataFrame
df.drop(df.columns[0], axis=1, inplace=True)

And you can use the following syntax to drop multiple columns from a pandas DataFrame by index numbers:

#drop first, second, and fourth column from DataFrame
cols = [0, 1, 3]
df.drop(df.columns[cols], axis=1, inplace=True)

If your DataFrame has duplicate column names, you can use the following syntax to drop a column by index number:

#define list of columns
cols = [x for x in range(df.shape[1])]

#drop second column
cols.remove(1)

#view resulting DataFrame
df.iloc[:, cols]

The following examples show how to drop columns by index in practice.

Example 1: Drop One Column by Index

The following code shows how to drop the first column in a pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],
                   'first': ['Dirk', 'Kobe', 'Tim', 'Lebron'],
                   'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'],
                   'points': [26, 31, 22, 29]})

#drop first column from DataFrame
df.drop(df.columns[0], axis=1, inplace=True)

#view resulting dataFrame
df

        first	last	 points
0	Dirk	Nowitzki 26
1	Kobe	Bryant	 31
2	Tim	Duncan	 22
3	Lebron	James	 29

Example 2: Drop Multiple Columns by Index

The following code shows how to drop multiple columns in a pandas DataFrame by index:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],
                   'first': ['Dirk', 'Kobe', 'Tim', 'Lebron'],
                   'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'],
                   'points': [26, 31, 22, 29]})

#drop first, second and fourth columns from DataFrame
cols = [0, 1, 3] 
df.drop(df.columns[cols], axis=1, inplace=True)

#view resulting dataFrame
df

        last
0	Nowitzki
1	Bryant
2	Duncan
3	James

Example 3: Drop One Column by Index with Duplicates

The following code shows how to drop a column by index number in a pandas DataFrame when duplicate column names exist:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],
                   'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'],
                   'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'],
                   'points': [26, 31, 22, 29]},
                   columns=['team', 'last', 'last', 'points'])

#define list of columns range
cols = [x for x in range(df.shape[1])]

#remove second column in DataFrame
cols.remove(1)

#view resulting DataFrame
df.iloc[:, cols]

	team	last	 points
0	Mavs	Nowitzki 26
1	Lakers	Bryant	 31
2	Spurs	Duncan	 22
3	Cavs	James	 29

Cite this article

stats writer (2024). How can I drop columns by their index in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-drop-columns-by-their-index-in-pandas/

stats writer. "How can I drop columns by their index in Pandas?." PSYCHOLOGICAL SCALES, 30 Apr. 2024, https://scales.arabpsychology.com/stats/how-can-i-drop-columns-by-their-index-in-pandas/.

stats writer. "How can I drop columns by their index in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-drop-columns-by-their-index-in-pandas/.

stats writer (2024) 'How can I drop columns by their index in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-drop-columns-by-their-index-in-pandas/.

[1] stats writer, "How can I drop columns by their index in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.

stats writer. How can I drop columns by their index in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top