• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:14 mins read
You are currently viewing How to Drop Multiple Columns by Index in Pandas

In Pandas, you can drop multiple columns by index using the drop() method. In this pandas drop multiple columns by index article, I will explain how to drop multiple columns by index with several DataFrame examples. You can drop columns by index by using the DataFrame.drop() method and by using DataFrame.iloc[].columns property to get the column names by index.

1. Quick Examples of Drop Multiple Columns by Index

If you are in a hurry, below are some quick examples of how to drop multiple columns by an index of DataFrame.


# Quick examples of drop multiple columns by index
                    
# Example 1: Drop columns based on column index
df2 = df.drop(df.columns[[0, 1, 2]],axis = 1)

# Example 2: Drop column of index 
# Using DataFrame.iloc[] and drop() methods
df2 = df.drop(df.iloc[:, 1:3],axis = 1)

# Example 3: Drop columns of index 
# Using DataFrame.loc[] and drop() methods
df2 = df.drop(df.loc[:, 'Courses':'Fee'].columns,axis = 1)

Now, let’s create a Pandas DataFrame with a few rows and columns and execute some examples, and validate results. Our DataFrame contains column names Courses, Fee and Discount.


# Create a Pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
    'Courses':["Spark","Spark","PySpark","JAVA","Hadoop",".Net","Python","AEM","Oracle","SQL DBA","C","WebTechnologies"],
    'Fee' :[22000,25000,23000,24000,26000,30000,27000,28000,35000,32000,20000,15000],
    'Duration':['30days','35days','40days','45days','50days','55days','60days','35days','30days','40days','50days','55days']
          }
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)

Yields below output.

pandas drop multiple index

2. Pandas Drop Multiple Columns By Index

In this section, you’ll learn how to drop multiple columns by index in pandas. You can use df.columns[[index1, index2, indexn]] to identify the list of column names in that index position and pass that list to the drop method.

Note that an index is 0-based. Use 0 to delete the first column and 1 to delete the second column and so on.


# Drop columns based on column index
df2 = df.drop(df.columns[[1, 2]],axis = 1)
print("Dropping multiple columns by index:\n", df2)  

Yields below output.

pandas drop multiple index

3. Drop Columns by Index Using iloc[] and drop() Methods

If you want to drop columns from starting and ending index ranges, you can do so by using the DataFrame.iloc[] property. This property returns all column names between specified indexes and passes these column names to the drop() method.


# Drop column of index using DataFrame.iloc[] and drop() methods.
df2 = df.drop(df.iloc[:, 1:3],axis = 1)
print("Dropping multiple columns by index:\n", df2)                      

Yields below output.


# Output:
# Dropping multiple columns by index:
            Courses
0             Spark
1             Spark
2           PySpark
3              JAVA
4            Hadoop
5              .Net
6            Python
7               AEM
8            Oracle
9           SQL DBA
10                C
11  WebTechnologies

4. Drop Columns by Index Using DataFrame.loc[] and drop() Methods

Similarly, you can drop columns by the range of labels using DataFrame.loc[] and DataFrame.drop() methods. Here the loc[] property is used to access a group of rows and columns by label(s) or a boolean array.


# Drop columns of index using DataFrame.loc[] and drop() methods.
df2 = df.drop(df.loc[:, 'Courses':'Fee'].columns,axis = 1)
print("Dropping multiple columns by index:\n", df2) 

Yields below output.


# Output:
# Dropping multiple columns by index:
   Duration
0    30days
1    35days
2    40days
3    45days
4    50days
5    55days
6    60days
7    35days
8    30days
9    40days
10   50days
11   55days

5. Complete Examples Drop Multiple Columns By Index


# Create a Pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
    'Courses':["Spark","Spark","PySpark","JAVA","Hadoop",".Net","Python","AEM","Oracle","SQL DBA","C","WebTechnologies"],
    'Fee' :[22000,25000,23000,24000,26000,30000,27000,28000,35000,32000,20000,15000],
    'Duration':['30days','35days','40days','45days','50days','55days','60days','35days','30days','40days','50days','55days']
          }
df = pd.DataFrame(technologies)
print(df)

# Drop Multiple Columns by labels
df2 = df.drop(['Courses', 'Duration'],axis = 1)
print(df2)
                    
# Drop columns based on column index
df2 = df.drop(df.columns[[0, 1, 2]],axis = 1)
print(df2)

# Drop column by index 
# Using DataFrame.iloc[] and drop() methods
df2 = df.drop(df.iloc[:, 1:3],axis = 1)
print(df2)

# Drop columns by labels 
# Using DataFrame.loc[] and drop() methods
df2 = df.drop(df.loc[:, 'Courses':'Fee'].columns,axis = 1)
print(df2)

Frequently Asked Questions on Drop Multiple Columns by Index

How do I drop multiple columns by index in Pandas?

To drop multiple columns by index in Pandas, you can use the drop method along with the column indices. For example, the columns_to_drop list contains the indices of the columns you want to drop (in this case, columns with indices 1 and 3). The axis=1 argument indicates that you are dropping columns, and inplace=True modifies the DataFrame in place.

What does the axis=1 parameter signify?

In Pandas, the axis parameter is used to specify whether an operation should be performed along rows (axis=0) or columns (axis=1). For the case of dropping columns by index using the drop method, the axis=1 parameter signifies that you are operating along columns. When you use axis=1, it indicates that the operation should be applied horizontally, affecting columns.

What does inplace=True do?

Setting inplace=True modifies the DataFrame in place, meaning it directly changes the existing DataFrame instead of creating a new one. If you set inplace=False (or omit it, as it defaults to False), a new DataFrame with the specified columns removed will be returned.

Is there an alternative method to drop columns by index?

An alternative method to drop columns by index in Pandas is to use the iloc method. The iloc method allows you to select rows and columns by integer position.

Conclusion

In this article, You have learned how to drop multiple columns by index using, DataFrame.drop() Method and DataFrame.iloc[] and DataFrame.loc[] properties with some examples.

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium