In Pandas, you can remove the first/last N columns from a DataFrame using various methods, such as drop()
with column names or indices. To remove the first or last n columns from the DataFrame using iloc[]
, drop()
, pop()
, or the del
keyword functions. In this article, I will explain how to drop the columns of the first and last n from Pandas DataFrame.
Key Points –
- Allows removing columns by index position, providing efficient slicing for the first or last N columns.
- Can remove columns by specifying labels or index positions, using the
axis=1
parameter for columns. - Removing columns based on their position can be done using index-based slicing techniques.
- The
drop()
method allows specifying columns by their names, useful when working with named data. - Removes one column at a time and returns the removed column, useful when sequentially removing multiple columns.
- Directly deletes a column by name, similar to
pop()
but doesn’t return the removed column. - For dropping the first N columns, use positional or label-based methods like
iloc[]
ordrop()
. - Similar to removing the first N, but with negative slicing or using the last indices in the column set.
Quick Examples of Remove First/Last N Columns
Below are quick examples of how to drop the first/last n columns.
# Quick examples of drop first/last N columns
# Number of columns to drop
n = 2
# Using DataFrame.iloc[]
# To drop last n columns
df2 = df.iloc[:, :-n]
# Using drop() function
# To delete last n columns
df.drop(columns=df.columns[-n:], axis=1, inplace=True)
# Using DataFrame.pop()
# To drop last n columns
for i in range(n):
df.pop(df.columns.values[-1])
# Using del keyword
# To drop last n columns
n = 3
for i in range(n):
del df[df.columns.values[-1]]
# Using DataFrame.iloc[]
# To drop first n columns
df2 = df.iloc[:,n:]
# Using drop() function
# To delete first n columns
df.drop(columns=df.columns[:n],axis=1, inplace=True)
# Using DataFrame.pop()
# To drop first n columns
for i in range(n):
df.pop(df.columns.values[0])
# Using del keyword
# To drop first n columns
for i in range(n):
del df[df.columns.values[0]]
To run some examples of dropping first/last N columns from Pandas DataFrame, let’s create a Pandas DataFrame.
# Create DataFrame
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas"],
'Fee' :[20000,25000,22000,24000],
'Duration':['30days','40days','35days','60days'],
'Discount':[1000,2300,2500,2000]
}
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print("Create DataFrame:\n", df)
Yields below output.
Drop Last N Columns From Pandas DataFrame
To drop the last n columns from the pandas DataFrame using either iloc[]
, drop()
, del keyword
, and pop()
methods.
Using iloc[]
You can use DataFrame.iloc[]
the indexing syntax [:,:-n]
with n as an integer to select the last n columns from pandas DataFrame. Let’s drop the last two columns of df and store the resulting DataFrame as a separate pandas DataFrame.
# Using DataFrame.iloc[]
# To drop last n columns
n = 2
df2 = df.iloc[:,:-n]
print("After dropping last n columns:\n", df2)
Output.
Using drop()
You can also use DataFrame.drop()
method to delete the last n columns. Use axis=1
to specify the columns and inplace=True
to apply the change on the existing DataFrame. On below example df.columns[-n:]
returns the last n column labels that would be deleting.
# Using drop() function
# To delete last n columns
n = 2
df.drop(columns=df.columns[-n:], axis=1, inplace=True)
print(df)
# Output:
# Courses Fee
# r1 Spark 20000
# r2 PySpark 25000
# r3 Python 22000
# r4 pandas 24000
Using pop()
Alternatively, You can also use for i in range(n): df.pop(df.columns.values[-1])
to remove the last n columns from Pandas DataFrame.
# Using dataframe.pop()
# To drop last n columns
n = 2
for i in range(n):
df.pop(df.columns.values[-1])
print(df)
# Output:
# Courses Fee
# r1 Spark 20000
# r2 PySpark 25000
# r3 Python 22000
# r4 pandas 24000
Using del keyword to Drop Last N Columns of DataFrame
By iterating over the last n column names of pandas DataFrame and for each of them select the column by passing column name in subscript operator to remove last N columns.
# Using del keyword
# To drop last n columns
n = 3
for i in range(n):
del df[df.columns.values[-1]]
print(df)
# Output:
# Courses
# r1 Spark
# r2 PySpark
# r3 Python
# r4 pandas
Delete First N Columns of Pandas DataFrame
Use iloc[]
, drop()
, pop()
, and del
keyword methods to drop the top/first n columns from the pandas DataFrame.
Using iloc[]
Use the DataFrame’s iloc[]
indexing syntax [:, n:]
, where n is an integer, to select all columns except the first n columns from a Pandas DataFrame. For instance, df.iloc[:, n:]
will exclude the first n columns, with n being the number of columns you want to remove.
# Using DataFrame.iloc[]
# To drop first n columns
n = 2
df2 = df.iloc[:,n:]
print(df2)
# Output:
# Courses Fee
# r1 Spark 20000
# r2 PySpark 25000
# r3 Python 22000
# r4 pandas 24000
Using drop()
You can also use DataFrame.drop()
method to remove the first n columns. Use columns
param to specify the columns and inplace=True
to apply the change on the existing DataFrame. In the below example df.columns[:n]
return the first n columns.
# Using drop() function
# To delete first n columns
n = 2
df.drop(columns=df.columns[:n],axis=1, inplace=True)
print(df)
# Output:
# Courses Fee
# r1 Spark 20000
# r2 PySpark 25000
# r3 Python 22000
# r4 pandas 24000
Using pop()
Alternatively, You can also use for i in range(n): df.pop(df.columns.values[0])
to delete the first n columns from pandas DataFrame.
# Using DataFrame.pop()
# To drop first n columns
n = 2
for i in range(n):
df.pop(df.columns.values[0])
print(df)
# Output:
# Courses Fee
# r1 Spark 20000
# r2 PySpark 25000
# r3 Python 22000
# r4 pandas 24000
Using the del keyword
By using the iterate over the first n column names of pandas DataFrame and for each of them select the column by passing column name in subscript operator to remove first n columns.
# Using del keyword
# To drop first n columns
n = 3
for i in range(n):
del df[df.columns.values[0]]
print(df)
# Output:
# Discount
# r1 1000
# r2 2300
# r3 2500
# r4 2000
Complete Example
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas"],
'Fee' :[20000,25000,22000,24000],
'Duration':['30days','40days','35days','60days'],
'Discount':[1000,2300,2500,2000]
}
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)
# Using DataFrame.iloc[]
# To drop last n columns
n = 2
df2 = df.iloc[: , :-n]
print(df2)
# Using drop() function
# To delete last n columns
n = 2
df.drop(columns=df.columns[-n:],
axis=1,
inplace=True)
print(df)
# Using DataFrame.pop()
# To drop last n columns
n = 2
for i in range(n):
df.pop(df.columns.values[-1])
print(df)
# Using del keyword
# To drop last n columns
n = 3
for i in range(n):
del df[df.columns.values[-1]]
print(df)
# Using DataFrame.iloc[]
# To drop first n columns
n = 2
df2 = df.iloc[:,n:]
print(df2)
# Using drop() function
# To delete first n columns
n = 2
df.drop(columns=df.columns[:n],
axis=1,
inplace=True)
print(df)
# Using DataFrame.pop()
# To drop first n columns
n = 2
for i in range(n):
df.pop(df.columns.values[0])
print(df)
# Using del keyword
# To drop first n columns
n = 3
for i in range(n):
del df[df.columns.values[0]]
print(df)
FAQs on Delete First/Last N Columns From DataFrame
To delete the last N columns from a pandas DataFrame, you can use the iloc
method with negative indexing. This code selects all rows (:
) and all columns up to the last N columns (:-n
). The negative indexing -n
specifies to exclude the last N columns.
To delete the first N columns from a pandas DataFrame, you can use the iloc
method. This code selects all rows (:
) and columns starting from the Nth column to the end. The iloc[:, n:]
part effectively excludes the first N columns.
You can drop both the first and last N columns from a Pandas DataFrame. To do this, you can combine the methods for dropping the first N columns and the last N columns.
You can delete specific columns from a pandas DataFrame by name using the DataFrame.drop()
method. This code will remove the specified columns ('Column1'
, 'Column2'
, and 'Column3'
) from your DataFrame. The drop()
method allows you to delete columns based on their names rather than their positions.
Conclusion
In this article, I have explained how to remove the first and last n columns from DataFrame using various methods such as iloc[]
, drop()
, pop()
, and del
keyword function with examples.
Happy Learning !!
Related Articles
- Drop First N Rows From Pandas DataFrame
- Drop Last N Rows From Pandas DataFrame
- Delete Last Row From Pandas DataFrame
- Pandas Drop Rows by Index
- How to drop the Pandas column by index?
- Drop Pandas first column from DataFrame.
- Drop multiple columns by index
- Pandas Drop the First Row of DataFrame
- How to drop Pandas Columns from DataFrame?