Pandas Drop First/Last N Columns From DataFrame

  • Post author:
  • Post category:Pandas
  • Post last modified:October 5, 2023

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 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 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 For Delete First/Last N Columns From 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(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)

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

I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this SparkByExamples.com to share my experiences with the data as I come across. You can learn more about me at LinkedIn

Leave a Reply

You are currently viewing Pandas Drop First/Last N Columns From DataFrame