Site icon Spark By {Examples}

Pandas Drop First Column From DataFrame

Spark spark.table() vs spark.read

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.

Key Points –

Related: In Pandas, you can drop the first/last n columns from DataFrame.

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

To run some examples of dropping the first column from DataFrame, let’s create Pandas DataFrame using data from a Python dictionary.


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

Pandas Drop First Column of DataFrame Using iloc[]

You can use DataFrame.iloc[] with 1 as the 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[]
# Using iloc[]
df2 = df.iloc[: , 1:]
print("After dropping the first column:\n", df2)

Yields below output.

pandas drop first column

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

Yields the same output as above.

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)

Yields the same output as above.

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.

Related: You can also drop the last column of the DataFrame.


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

Yields the same output as above.

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)

Frequently Asked Questions on Drop First Column from Pandas DataFrame

How do I drop the first column from a Pandas DataFrame?

To drop the first column from a DataFrame, you can use the drop() method along with the column name and specify axis=1 to indicate that you are dropping a column. For example, df1 = df.drop(df.columns[0], axis=1)

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)

Is there a way to drop the first column in place without assigning to a new variable?

You can use the inplace=True parameter to modify the existing DataFrame in place instead of creating a copy of the DataFrame. For example, df.drop(df.columns[0], axis=1, inplace=True)

How do I drop multiple columns, including the first one?

If you want to drop multiple columns, including the first one, you can pass a list of column names to the drop() method: For example, columns_to_drop = [df.columns[0], 'Column2', 'Column3']
df = df.drop(columns = columns_to_drop)

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:]

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

Exit mobile version