Pandas Drop First/Last N Columns From DataFrame

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 18, 2023
Spread the love

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/delete the first and last n columns from Pandas DataFrame with examples.

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 results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


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)

Yields below output.


    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500
r4   pandas  24000   60days      2000

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(df2)

Yields below output.


r1    Spark  20000
r2  PySpark  25000
r3   Python  22000
r4   pandas  24000

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


    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.


   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 same output as above.

3.4 Using del keyword to Drop First N Columns of Pandas DataFrame

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.


    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

Leave a Reply

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