To drop the first column from a pandas DataFrame, you can use the drop()
method along with specifying the column name or index. In this article, I will explain how to remove the first column from Pandas DataFrame by using either iloc[]
property, drop()
, pop()
functions, and del
keyword.
Key Points –
- Use the
drop()
function in pandas to remove 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.
Related: In Pandas, you can drop the first/last n columns from DataFrame.
Quick Examples of Drop First Column
If you are in a hurry, below are some quick examples of how to remove 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])
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
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.
Drop First Column of DataFrame Using iloc[]
To drop the first column of a DataFrame using iloc[], you can select all rows (:
) and all columns starting from the second column index onwards.
# 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.
Drop First Column of DataFrame Using drop()
To drop the first column of a DataFrame using the drop()
function, 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 on Drop First Column from Pandas DataFrame
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)
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)
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)
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 conclusion, we’ve explored various methods to drop the first column from a Pandas DataFrame, including DataFrame.iloc[]
, DataFrame.drop()
, DataFrame.pop()
, and the del
keyword. Each method offers its own advantages and can be selected based on specific use cases and preferences.
Happy Learning !!
Related Articles
- Pandas Drop Rows by Index
- Drop multiple columns by index
- Pandas Series loc[] Function
- Pandas Difference Between loc[] vs iloc[]
- Pandas loc[] multiple conditions
- Pandas Delete Last Row From DataFrame
- Pandas Drop Last N Rows From DataFrame
- Pandas Drop First N Rows From DataFrame
- How to drop first row from the Pandas DataFrame
- Pandas Drop First Three Rows From DataFrame
- Pandas Drop Multiple Columns From DataFrame
- How to Drop Duplicate Columns in Pandas DataFrame?
- How to drop the Pandas column by index?
- Pandas Drop Columns with NaN or None Values