How to Drop Column by Index in Pandas

When I first started working with large financial datasets, I often found myself stuck with extra columns that didn’t have clear names. Sometimes, you just know that the third and fifth columns are junk and you need them gone.

In this tutorial, I will show you exactly how to drop a column by index in Pandas using the same techniques I use in my daily data engineering work.

Use the drop() Method with df.columns

The most common way I drop columns by their position is by combining the drop() method with the df.columns property. Since the drop() function usually looks for column names, we have to “translate” the index number into a name first.

I prefer this method because it is explicit and very easy for other developers to read when they look at your code later.

import pandas as pd

# Creating a sample dataset of US Tech Companies
data = {
    'Company': ['Apple', 'Microsoft', 'Alphabet', 'Amazon'],
    'Ticker': ['AAPL', 'MSFT', 'GOOGL', 'AMZN'],
    'Market_Cap_B': [3000, 2800, 1700, 1600],
    'State': ['California', 'Washington', 'California', 'Washington'],
    'Founded': [1976, 1975, 1998, 1994]
}

df = pd.DataFrame(data)

# Let's say I want to drop the 'State' column which is at index 3
# Remember: Pandas indices start at 0
df_cleaned = df.drop(df.columns[3], axis=1)

print("Original DataFrame:")
print(df)
print("\nDataFrame after dropping column at index 3:")
print(df_cleaned)

I executed the above example code and added the screenshot below.

Drop Column by Index in Pandas

In the example above, df.columns[3] identifies the name “State” for me. By setting axis=1, I tell Pandas to look at the columns rather than the rows.

Drop Multiple Columns by Index

There are plenty of times when I need to clear out multiple columns at once. Instead of writing separate lines for each, I pass a list of the index positions I want to remove.

I find this particularly useful when dealing with exported CSV files from US government databases where the first few columns are often metadata I don’t need.

import pandas as pd

# Sample Real Estate data from various US cities
data = {
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston'],
    'Median_Home_Price': [800000, 950000, 320000, 300000],
    'Unused_ID': [101, 102, 103, 104],
    'Tax_Rate': [1.2, 1.1, 2.1, 1.8],
    'Internal_Code': ['A1', 'B2', 'C3', 'D4']
}

df = pd.DataFrame(data)

# Dropping 'Unused_ID' (index 2) and 'Internal_Code' (index 4)
cols_to_drop = [2, 4]
df_refined = df.drop(df.columns[cols_to_drop], axis=1)

print(df_refined)

I executed the above example code and added the screenshot below.

How to Drop Column by Index in Pandas

Using a list like df.columns[[2, 4]] allows you to pluck out exactly what you need to discard in a single, clean operation.

Drop a Range of Columns Using iloc

If you have a large block of columns that need to go, using a range is much faster than listing every single number. I typically use the iloc property for this.

For instance, if I have a dataset with 50 columns and I want to get rid of everything from index 10 to 20, slicing is the way to go.

import pandas as pd
import numpy as np

# Creating a larger US Census-style dataset
data = {
    'State': ['NY', 'CA', 'TX', 'FL', 'IL'],
    'Pop_2020': [20.2, 39.5, 29.1, 21.5, 12.8],
    'Dummy_1': [0, 0, 0, 0, 0],
    'Dummy_2': [1, 1, 1, 1, 1],
    'Dummy_3': [2, 2, 2, 2, 2],
    'Avg_Income': [75000, 80000, 65000, 60000, 70000]
}

df = pd.DataFrame(data)

# Dropping the 'Dummy' columns located from index 2 up to index 5
# iloc[:, 2:5] selects those columns, and then we get their names
df_final = df.drop(df.iloc[:, 2:5], axis=1)

print(df_final)

I executed the above example code and added the screenshot below.

Drop Pandas Column by Index

One thing to keep in mind is that iloc slicing is exclusive of the end index. So 2:5 will drop columns 2, 3, and 4.

Use the inplace Parameter

By default, Pandas creates a new DataFrame when you drop a column. This is great for keeping your original data safe, but if you are working with a massive dataset, it can eat up your memory.

I often use inplace=True when I’m sure I won’t need those columns again and want to save resources.

import pandas as pd

# US Automotive Sales Data
data = {
    'Model': ['Tesla Model 3', 'Ford F-150', 'Chevy Silverado'],
    'Units_Sold': [12000, 45000, 38000],
    'Region': ['North', 'South', 'West']
}

df = pd.DataFrame(data)

# Dropping the 'Region' column (index 2) directly on the original df
df.drop(df.columns[2], axis=1, inplace=True)

# No need to assign to a new variable
print(df)

When you use inplace=True, the function returns None, and the changes happen directly to the df variable.

Handle Errors with errors=’ignore’

Sometimes my code runs on different versions of a dataset where a column might or might not exist at a certain position. To prevent my whole script from crashing, I use the errors parameter.

This has saved me from countless late-night debugging sessions when a data source slightly changed its format.

import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

try:
    # Attempting to drop a non-existent index safely
    df.drop(df.columns[5], axis=1, errors='ignore')
    print("Code ran without crashing!")
except Exception as e:
    print(f"Error: {e}")

Setting errors=’ignore’ ensures that if the column index is out of bounds or the label isn’t found, the code simply moves on.

I hope you found this tutorial helpful! Dropping columns by index is a fundamental skill that makes data cleaning much faster, especially when names are messy or missing.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.