• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:18 mins read
You are currently viewing Pandas Drop First/Last N Columns From DataFrame

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.

delete first N Columns

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.

delete first N Columns

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

How do I delete the last N columns from a pandas 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.

How can I delete the first N columns from a pandas DataFrame?

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.

Can I drop both first and last N columns from a DataFrame?

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.

Can I modify the original DataFrame instead of creating a new one?

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.

Can I delete specific columns by name instead of position?

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 !!

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

Leave a Reply