5.3 C
London
Thursday, December 19, 2024
HomePandas in PythonDataFrame Functions in PythonHow to Drop Columns by Index in Pandas

How to Drop Columns by Index in Pandas

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Additional Resources

How to Combine Two Columns in Pandas
Pandas: How to Sort Columns by Name
Pandas: How to Find the Difference Between Two Columns
Pandas: How to Sum Columns Based on a Condition

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories