Pandas Drop First Column From DataFrame

Spread the love

To drop the first column from the pandas DataFrame use either iloc[] property, drop(), pop() functions, and del keyword. In this article, I will explain how to drop/delete/remove the first column from Pandas DataFrame with examples.

  • drop() function is used to remove columns or rows from DataFrame;
  • Use axis param to specify what axis you would like to delete. By default axis=0 meaning to remove rows. Use axis=1 or columns param to remove columns.
  • Use inplace=True to remove row/column in place meaning on existing DataFrame without creating copy.

1. Quick Examples of Drop First Column From DataFrame

If you are in a hurry, below are some quick examples of how to drop/delete the first column from pandas DataFrame.


# Below are quick example

# 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])

Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are CoursesFeeDuration and Discount.


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)

Yields below output.


# Output
    Courses    Fee Duration  Discount
r1    Spark  22000   30days      1000
r2  PySpark  25000   50days      2000
r3   Hadoop  30000   40days      2500
r4   Pandas  35000   35days      1500

2. Pandas Drop First Column of DataFrame Using iloc[]

Use DataFrame.iloc[] with 1 as input you can drop the first column from Pandas DataFrame, For example use, df.iloc[:,1:] to select all columns except the first one and then assign it back to the original variable which ideally drops the first column from DataFrame.


# 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)

Yields below output.


# Output
      Fee Duration  Discount
r1  22000   30days      1000
r2  25000   50days      2000
r3  30000   40days      2500
r4  35000   35days      1500

3. Drop First Column of DataFrame Using drop()

You can also use DataFrame.drop() function to remove the first column. Use axis=1 to specify the columns and inplace=True to apply the change on the existing DataFrame. In the below example, df.columns[0] returns Courses which is the first column from our DataFrame.


# 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)

Yields the same output as above.

4. Use del Keyword to Drop 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(df)

Yields the same output as above.

5. Drop First Column of DataFrame Using pop()

Similarly, you can use DataFrame.pop() function to drop the first column of pandas DataFrame and also returned the remove column as a series. So, you can select the first column of DataFrame by using df.columns[0] and pass 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(df)

Yields the same output as above.

6. Complete Example For Drop First Column From 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(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)

7. Conclusion

In this article, I have explained how to drop the first column from Pandas DataFrame using DataFrame.iloc[], DataFrame.drop(), and DataFrame.pop(), and del Keyword functions with examples.

Happy Learning !!

Related Articles

References

Leave a Reply

You are currently viewing Pandas Drop First Column From DataFrame