• Post author:
  • Post category:Pandas
  • Post last modified:May 29, 2024
  • Reading time:14 mins read
You are currently viewing Pandas Drop First Column From DataFrame

To remove the first column from a pandas DataFrame, you can use the drop() method by specifying the column name or its index. In this article, I will explain remove the first column from Pandas DataFrame by using either iloc[] property, drop(), pop() functions, and del keyword.

Advertisements

Key Points –

  • Use the drop() method in pandas to delete columns from a DataFrame.
  • Specify the column to drop either by its name or index using the appropriate parameter.
  • Set axis=1 to indicate that you’re dropping a column (by default, axis=0 drops rows).
  • Utilize the drop() method with the appropriate axis parameter (axis=1 or columns) to remove columns from the DataFrame.
  • Access column labels using the .columns attribute and column indexes using integer positions.

Quick Examples of Drop First Column

Following are quick examples of removing the first column from Pandas DataFrame.


# Quick examples of drop first column

# Example 1: Drop first column of dataframe 
# Using iloc[]
df2 = df.iloc[: , 1:]

# Example 2: select all columns
# Except the first column
df2 = df[df.columns[1:]]

# Example 3: Drop first column of dataframe 
# Using drop()
df.drop(columns=df.columns[0], axis=1,  inplace=True)

# Example 4: Using drop() function 
# To delete first column
df2 = df.drop(df.columns[0],axis=1)

# Example 5: Use del keywords to drop 
# First column of dataframe
del df[df.columns[0]]

# Example 6: Drop first column of dataframe 
# Using pop()
df.pop(df.columns[0])

First, let’s create a Pandas DataFrame.


import pandas as pd
import numpy as np
technologies= ({
    'Courses':["Spark","PySpark","Hadoop","Pandas"],
    'Fee': [22000,25000,30000,35000],
    'Duration':['30days','50days','40days','35days'],
    'Discount':[1000,2000,2500,1500]
              })
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print("DataFrame:\n", df)

Yields below output.

pandas drop first column

Drop First Column of DataFrame Using iloc[]

To drop the first column of a DataFrame using iloc[], you can select all columns except the first one.


# Drop first column of dataframe 
# Using iloc[]
df2 = df.iloc[: , 1:]
print("After dropping the first column:\n", df2)

In the above examples, df.iloc[:, 1:] selects all rows (:) and all columns starting from the second column index (1) onwards. By assigning this selection back to the variable df, you effectively drop the first column from the DataFrame.

pandas drop first column

Drop First Column of DataFrame Using drop()

To drop the first column of a DataFrame using the drop() method, you can specify the column to drop either by its name or index.


# Drop first column of dataframe using drop()
df.drop(columns=df.columns[0], axis=1,  inplace=True)
print("After dropping the first column:\n", df)

# Using drop() function to delete first column
df2 = df.drop(df.columns[0],axis=1)
print("After dropping the first column:\n", df2)

# Output:
# After dropping the first column:
#        Fee Duration  Discount
# r1  22000   30days      1000
# r2  25000   50days      2000
# r3  30000   40days      2500
# r4  35000   35days      1500

In the above examples, drop the first column of the DataFrame df using the drop() function. df.columns[0] selects the name of the first column, and axis=1 specifies that you are dropping a column. Finally, by assigning the result back to the variable df, the first column is effectively dropped from the DataFrame.

Use del Keyword to Drop the First Column

Alternatively, you can also use del df[df.columns[0]] to remove the first column of the Pandas DataFrame. Here, it will select all columns except the first column of DataFrame.


# Use del keywords to drop 
# First column of dataframe
del df[df.columns[0]]
print("After dropping the first column:\n", df)

# Output:
# After dropping the first column:
#        Fee Duration  Discount
# r1  22000   30days      1000
# r2  25000   50days      2000
# r3  30000   40days      2500
# r4  35000   35days      1500

In the above examples, del df[df.columns[0]] remove the first column of the DataFrame df by selecting it using df.columns[0]. After executing this line, the first column is deleted from the DataFrame.

Drop the First Column of DataFrame Using pop()

Similarly, you can use DataFrame.pop() function to drop the first column of pandas DataFrame and also return the remove column as a series. So, you can select the first column of DataFrame by using df.columns[0] and passing this to the pop(), it will select all columns except the first column of DataFrame.


# Drop first column of dataframe using pop()
df.pop(df.columns[0])
print("After dropping the first column:\n", df)

# Output:
# After dropping the first column:
#        Fee Duration  Discount
# r1  22000   30days      1000
# r2  25000   50days      2000
# r3  30000   40days      2500
# r4  35000   35days      1500

In the above examples, df.pop(df.columns[0]) drops the first column of the DataFrame df and returns it. The returned column is assigned to the variable df, and the modified DataFrame without the first column is printed.

Complete Example For Remove First Column


import pandas as pd
import numpy as np
technologies= ({
    'Courses':["Spark","PySpark","Hadoop","Pandas"],
    'Fee': [22000,25000,30000,35000],
    'Duration':['30days','50days','40days','35days'],
    'Discount':[1000,2000,2500,1500]
              })
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

# Drop first column of dataframe 
# Using iloc[]
df2 = df.iloc[: , 1:]
print(df2)

# Select all columns
# Except the first column
df2 = df[df.columns[1:]]
print(df2)

# Drop first column of dataframe using drop()
df.drop(columns=df.columns[0], axis=1,  inplace=True)
print(df)

# Using drop() function to delete first column
df2 = df.drop(df.columns[0],axis=1)
print(df2)

# Use del keywords to drop 
# First column of dataframe
del df[df.columns[0]]
print(df)

# Drop first column of dataframe 
# Using pop()
df.pop(df.columns[0])
print(df)

Frequently Asked Questions

How can I drop the first column without specifying the column name?

You can use integer indexing to drop the first column without explicitly specifying the column name. For example, df1 = df.drop(df.columns[0], axis=1)

What if I don’t know the name of the first column?

If you don’t know the name of the first column, you can access it using df.columns[0] and then use the drop() method.

Can I drop multiple columns at once using the drop() method?

You can drop multiple columns at once by passing a list of column names to the drop() method.

What does axis=1 mean in the drop() method?

The axis parameter in the drop() method specifies whether to drop labels from the rows (axis=0) or columns (axis=1). When you set axis=1, you are indicating that you want to drop a column.

Is there a way to drop the first N columns?

You can use the slicing technique to drop the first N columns from the DataFrame. For example,
n = 3
df = df.iloc[:, n:]

Conclusion

In this article, we explored various functions to drop the first column from a Pandas DataFrame. We demonstrated how to use iloc[], drop(), pop(), and the del keyword, each with practical examples.

Happy Learning !!

Related Articles

References