How to drop column(s) by index in Pandas? You can use the drop()
function in Pandas to remove columns by index. Set the axis
parameter to 1 (indicating columns) and specify either the single-column index or a list of column indices you want to eliminate. In this article, I will explain how to drop column(s) by index using multiple ways of pandas such as the DataFrame.drop() function, DataFrame.loc[]
function and DataFrame.iloc[].columns
property.
Key Points –
- Use the
DataFrame.drop()
method with theaxis
parameter set to 1 to drop columns by index. - Specify the column index or a list of column indices to drop multiple columns at once.
- Dropping columns by index modifies the DataFrame in place unless the
inplace
parameter is set to False. - Provide either a single-column index or a list of column indices to be dropped.
- To drop multiple columns, pass a list containing the indices of the columns you want to drop.
Quick Examples of Dropping Columns by Index
Below are some quick examples of dropping column(s) by an index in pandas.
# Quick examples of dropping columns by index
# Example 1: Using DataFrame.drop() method
df2=df.drop(df.columns[1], axis=1)
# Example 2: Drop first column with param inplace = True
df.drop(df.columns[1], axis=1, inplace = True)
# Example 3: Drop columns based on column index
df2 = df.drop(df.columns[[0, 1, 2]],axis = 1)
# Example 4: Drop column of index
# Using DataFrame.iloc[] and drop() methods
df2 = df.drop(df.iloc[:, 1:3],axis = 1)
# Example 5: Drop columns by labels
# Using DataFrame.loc[] and drop() methods
df2 = df.drop(df.loc[:, 'Courses':'Fee'].columns,axis = 1)
To run some examples of drop column(s) by index. let’s create DataFrame using data from a dictionary.
# 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.
Using DataFrame.drop() Column by Index
You can use DataFrame.drop() function to remove the column by index. The drop()
function is used to drop specified labels from rows or columns. Remove rows or columns by specifying label names and corresponding axes, or by specifying the direct index of columns. When using a multi-index, labels on different levels can be removed by specifying the level.
# Using DataFrame.drop() method.
df2=df.drop(df.columns[1], axis=1)
print("After dropping the first column:\n", df2)
Yields below output. This deletes the second column as the index starts from 0.
If you want to update the existing DataFrame instead of creating a new DataFrame after dropping it’s column you can use the inplace=True
parameter of the drop()
function. This function will return the original DataFrame with the remaining columns.
# Drop first column with param inplace = True
df.drop(df.columns[1], axis=1, inplace = True)
print("After dropping the first column:\n", df)
Yields below output.
# Output:
After dropping the first column:
Courses Duration
0 Spark 30days
1 Spark 35days
2 PySpark 40days
3 JAVA 45days
4 Hadoop 50days
5 .Net 55days
6 Python 60days
7 AEM 35days
8 Oracle 30days
9 SQL DBA 40days
10 C 50days
11 WebTechnologies 55days
Drop Multiple Columns By Index
In this section, you’ll learn how to drop multiple columns by index. 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.
# Drop multiple columns based on column index
df2 = df.drop(df.columns[[1, 2]],axis = 1)
print("After dropping multiple columns:\n", df2)
Yields below output.
# Output:
After dropping multiple columns:
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
Drop Columns Using DataFrame.iloc[] and drop() Methods
To drop columns using DataFrame.iloc[]
and drop()
methods, you specify the index positions of the columns you want to drop using iloc[]
. For instance, df.iloc[:, 1:3]
selects all rows (:
) and columns from index position 1 up to, but not including, index position 3. This selects columns at index positions 1 and 2, which are Fee
and Duration
. Then you can use df.drop()
to drop these selected columns along the specified axis (axis=1 for columns).
# Drop column of index
# Using DataFrame.iloc[] and drop() methods
df2 = df.drop(df.iloc[:, 1:3],axis = 1)
print("After dropping multiple columns:\n", df2)
Yields below output.
After dropping multiple columns:
# Output:
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
Drop Columns of Index Using DataFrame.loc[] and drop() Methods
Similarly, to drop columns using DataFrame.loc[]
and the drop()
method, you specify the range of column labels you want to drop using loc[]
. For instance, df.loc[:, Courses:Fee]
select all rows (:
) and columns from Courses
to Fee
using label-based indexing. .columns
returns the column labels within the specified range. Then you can use df.drop()
to drop the columns obtained from the previous step along the specified axis (axis=1
for columns).
# Drop columns of index
# Using DataFrame.loc[] and drop() methods.
df2 = df.drop(df.loc[:, 'Courses':'Fee'].columns,axis = 1)
print("After dropping multiple columns:\n", df2)
# Drop columns of index
# Using DataFrame.loc[] and drop() methods
columns_to_drop = df.loc[:, 'Courses':'Fee'].columns
df2 = df.drop(columns_to_drop, axis=1)
print("After dropping multiple columns:\n", df2)
Yields below output.
# Output:
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 of Drop 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)
# Using DataFrame.drop() method.
df2=df.drop(df.columns[1], axis=1)
print(df2)
# 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)
FAQ on Drop Columns by Index
Use the DataFrame.drop()
method with the column index specified in the columns
parameter and set the axis
parameter to 1. For example: df.drop(columns=df.columns[index], axis=1)
.
You can drop multiple non-contiguous columns by specifying a list of column indexes within the df.columns
accessor. For example, df = df.drop(df.columns[[0, 2, 4]], axis=1)
Provide a list of column indices to the columns
parameter in the drop()
method and set axis=1
. For example: df.drop(columns=[df.columns[index1], df.columns[index2]], axis=1)
.
If you prefer to use column names instead of indexes, you can directly specify the column names within the drop()
method. For example, df = df.drop(['
column1′, ‘column3’], axis=1)
By default, drop()
does not modify the DataFrame in place. To modify the DataFrame in place, set the inplace
parameter to True
.
Conclusion
In this article, You have learned how to drop column(s) by index in pandas by using drop()
, iloc[]
, and loc[]
properties with examples.
Related Articles
- Change the Order of Pandas DataFrame Columns
- How to Change the Position of a Column in Pandas
- Pandas Shuffle DataFrame Rows Examples
- Convert String to Float in Pandas DataFrame
- Convert Float to Integer in Pandas DataFrame
- How to Unpivot DataFrame in Pandas?
- Pandas Create New DataFrame By Selecting Specific Columns