You can drop multiple columns by index in Pandas using the DataFrame.drop()
method. Simply specify the column indices you want to remove as a list. In this pandas drop multiple columns by index article, I will explain dropping multiple columns by index with several DataFrame examples. You can drop columns by index by using the drop()
method and by using DataFrame.iloc[].columns
property to get the column names by index.
Key Points –
- Use the
DataFrame.drop()
method in Pandas to remove columns by index. - Specify the indices of the columns you want to drop as a list.
- Use
axis=1
to indicate that you’re dropping columns (rows are dropped withaxis=0
). - Set
inplace=True
to modify the DataFrame in place; otherwise, it returns a new DataFrame with the specified columns dropped.
Quick Examples of Drop Multiple Columns by Index
Following are quick examples of dropping multiple columns by an index.
# 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)
First, let’s create a DataFrame.
# 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.
Remove Several Columns By Index
To drop columns based on column index in Pandas, you can directly use the DataFrame.drop()
method specifying the column indices you want to drop. For instance, the df.columns[[1, 2]]
selects the columns at indices 1 and 2 (columns ‘Fee’ and ‘Duration’ in this case), and then .drop()
method is used to remove them from the DataFrame.
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.
Drop Columns by Index Using iloc[] and drop() Methods
Similarly, 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
Use loc[] & drop() Functions
To remove columns by index in Pandas using the DataFrame.loc[] and drop() methods, you can indicate the column range to be eliminated with the loc[]
method and then use the drop()
method to remove them. The loc[]
method is generally used to retrieve a set of rows and columns based on labels 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
Complete Examples
# 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
You can drop multiple columns by their index positions using the DataFrame.drop()
method along with column indices obtained through various methods like iloc[]
, loc[]
, or direct slicing.
While loc[]
is primarily used for label-based indexing, you can indirectly drop columns by index using it.
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.
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.
Conclusion
In conclusion, you have explored various methods to drop multiple columns by index in a Pandas DataFrame. By using the DataFrame.drop()
method along with DataFrame.iloc[]
or DataFrame.loc[]
, you can efficiently remove columns based on their indices.
Related Articles
- Pandas Series loc[] attribute
- Pandas Sort by Column Values DataFrame
- Pandas iterate over the columns Of DataFrame
- Pandas Drop the First Row of the DataFrame
- How to drop Pandas Columns from DataFrame?
- Pandas Drop Multiple Columns From DataFrame
- How to Drop Duplicate Columns in Pandas DataFrame?
- Drop Pandas first column from DataFrame