In this article, we will discuss different ways to delete first column of a pandas dataframe in python.
Table of Contents
- Use iloc to drop first column of pandas dataframe.
- Use drop() to remove first column of pandas dataframe.
- Use del keyword to remove first column of pandas dataframe.
- Use pop() to remove first column of pandas dataframe.
Use iloc to drop first column of pandas dataframe
In Pandas, the dataframe provides an attribute iloc, to select a portion of the dataframe using position based indexing. This selected portion can be few columns or rows . We can use this attribute to select all the columns except the first one and then assign back the selected columns to the original variable. It will give an effect that we have deleted the first column from the dataframe. For example,
# Drop first column of dataframe df = df.iloc[: , 1:]
We selected a portion of dataframe, that included all rows, but it selected only n-1 columns i.e. from first column onwards. Then assigned this back to the same variable. So, basically it removed the first column of dataframe.
How did it work?
The syntax of dataframe.iloc[] is like,
df.iloc[row_start:row_end , col_start, col_end]
Arguments:
- row_start: The row index/position from where it should start selection. Default is 0.
- row_end: The row index/position from where it should end the selection i.e. select till row_end-1. Default is till the last row of the dataframe.
- col_start: The column index/position from where it should start selection. Default is 0.
- col_end: The column index/position from where it should end the selection i.e. select till end-1. Default is till the last column of the dataframe.
It returns a portion of dataframe that includes rows from row_start to row_end-1 and columns from col_start to col_end-1.
To delete the first column from dataframe, just selected the columns from column number 2 till the end and select all rows. As indexing starts from 0, so to select all columns after the first one use –> (1:) i.e. from 2nd column till end. To select all the rows use default values i.e. (:) i.e.
df = df.iloc[: , 1:]
Checkout complete example to delete the first column of dataframe,
import pandas as pd # List of Tuples empoyees = [('Jack', 34, 'Sydney', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aadi', 16, 'London', 11) , ('Mark', 41, 'Delhi' , 12)] # Create a DataFrame object df = pd.DataFrame( empoyees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) # Drop first column of dataframe df = df.iloc[: , 1:] print("Modified Dataframe : ") print(df)
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12 Modified Dataframe : Age City Experience 0 34 Sydney 5 1 31 Delhi 7 2 16 London 11 3 41 Delhi 12
Use drop() to remove first column of pandas dataframe
In pandas, the dataframe’s drop() function accepts a sequence of column names that it needs to delete from the dataframe. To make sure that it removes the columns only, use argument axis=1 and to make changes in place i.e. in calling dataframe object, pass argument inplace=True.
Checkout complete example to delete the first column of dataframe is as follows,
import pandas as pd # List of Tuples empoyees = [('Jack', 34, 'Sydney', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aadi', 16, 'London', 11) , ('Mark', 41, 'Delhi' , 12)] # Create a DataFrame object df = pd.DataFrame( empoyees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) # Drop first column df.drop(columns=df.columns[0], axis=1, inplace=True) print("Modified Dataframe : ") print(df)
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12 Modified Dataframe : Age City Experience 0 34 Sydney 5 1 31 Delhi 7 2 16 London 11 3 41 Delhi 12
We fetched the column names of dataframe as a sequence and passed the first column name as the columns argument in drop() function, therefore it deleted the first column of dataframe.
Use del keyword to drop first column of pandas dataframe
Fetch the name of first column of dataframe i.e. at position 0, from the dataframe.columns sequence. Then select that column by passing column name in subscript operator i.e. df[df.columns[0]]. Then call del keyword on selected column,
del df[df.columns[0]]
It will delete the first column of dataframe.
Checkout complete example to remove the first column of dataframe is as follows,
import pandas as pd # List of Tuples empoyees = [('Jack', 34, 'Sydney', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aadi', 16, 'London', 11) , ('Mark', 41, 'Delhi' , 12)] # Create a DataFrame object df = pd.DataFrame( empoyees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) # Delete first column del df[df.columns[0]] print("Modified Dataframe : ") print(df)
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12 Modified Dataframe : Age City Experience 0 34 Sydney 5 1 31 Delhi 7 2 16 London 11 3 41 Delhi 12
It deleted the first column of dataframe in place.
Use pop() to drop first column of pandas dataframe
In Pandas, dataframe provides a function pop(column_name). It expects a column name as an argument and deletes that column from the calling dataframe object. It also returns the deleted column as a series. Let’s use this to delete first column of dataframe,
import pandas as pd # List of Tuples empoyees = [('Jack', 34, 'Sydney', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aadi', 16, 'London', 11) , ('Mark', 41, 'Delhi' , 12)] # Create a DataFrame object df = pd.DataFrame( empoyees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) # Drop first column deleted_column = df.pop(df.columns[0]) print("Modified Dataframe : ") print(df)
Output:
Name Age City Experience 0 Jack 34 Sydney 5 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12 Modified Dataframe : Age City Experience 0 34 Sydney 5 1 31 Delhi 7 2 16 London 11 3 41 Delhi 12
It removed the first column of dataframe in place and also returned the deleted column as a series.
Summary:
We learned about different ways to delete the first column of a dataframe.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.