In Pandas, you can drop the first/last N columns from a DataFrame using the drop()
method along with the column names or column indices. To drop the first or last n columns from the pandas DataFrame using either iloc[]
, drop()
, pop()
, and del
keyword methods. In this article, I will explain how to drop the columns of the first and last n from Pandas DataFrame with examples.
Related: In Pandas, You can drop the first column/drop the last column of the DataFrame.
1. Quick Examples of Drop First/Last N Columns From DataFrame
If you are in a hurry, below are some quick examples of how to drop the first/last n columns from Pandas DataFrame.
# Below are a quick example
# 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]]
Now, let’s create a DataFrame with a few rows and columns, execute these examples, and validate the results. Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
# 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.
2. 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.
2.1 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)
Yields below output.
2.2 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)
Yields the same output as above.
2.3 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)
Yields the same output as above.
2.4 Using del keyword to Drop Last N Columns of Pandas DataFra
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)
Yields below output.
# Output:
Courses
r1 Spark
r2 PySpark
r3 Python
r4 pandas
3. 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.
3.1 Using iloc[]
Use DataFrame.iloc[]
the indexing syntax [:,n:]
with n as an integer to select the first n columns from pandas DataFrame. For example df.iloc[:,n:]
, substitute n with the integer number specifying how many columns you wanted to delete.
# Using DataFrame.iloc[] to drop first n columns
n = 2
df2 = df.iloc[:,n:]
print(df2)
Yields below output.
# Output:
Duration Discount
r1 30days 1000
r2 40days 2300
r3 35days 2500
r4 60days 2000
3.2 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)
Yields the same output as above.
3.3 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)
Yields the same output as above.
3.4 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)
Yields below output.
# Output:
Discount
r1 1000
r2 2300
r3 2500
r4 2000
4. 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)
Frequently Asked Questions 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 modify the original DataFrame in place instead of creating a new one. To do this, you can use the inplace=True
parameter with the drop
method.
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, you have learned how to remove the first and last n columns from Pandas DataFrame using DataFrame.iloc[]
, DataFrame.drop()
, DataFrame.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.
- Pandas Drop Columns with NaN or None Values
- Drop the last column from the DataFrame
- Drop multiple columns by index
- Pandas Drop the First Row of DataFrame
- How to drop Pandas Columns from DataFrame?
- Pandas Drop Multiple Columns From DataFrame
- How to Drop Duplicate Columns in Pandas DataFrame?