In this article, we will discuss different ways to delete first row of a pandas dataframe in python.
Table of Contents
- Use iloc to drop first row of pandas dataframe.
- Use drop() to remove first row of pandas dataframe.
- Use tail() function to remove first row of pandas dataframe.
Use iloc to drop first row 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 rows except the first one and then assign back the selected rows to the original variable. It will give an effect that we have deleted the first row from the dataframe. For example,
# Drop first row # by selecting all rows from first row onwards df = df.iloc[1: , :]
We selected a portion of dataframe, that included all columns, but it selected only n-1 rows i.e. from first row onwards. Then assigned this back to the same variable. So, basically it removed the first row 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 row from dataframe, just selected the rows from row number 2 till the end and select all columns. As indexing starts from 0, so to select all rows after the first one use –> (1:) i.e. from 2nd row till end. To select all the columns use default values i.e. (:) i.e.
df = df.iloc[1: , :]
Checkout complete example to delete the first row 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 row # by selecting all rows from first row onwards 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 : Name Age City Experience 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12
Most frequently asked Pandas queries,
- Drop last row of pandas dataframe in python.
- Pandas: Drop first N rows of dataframe.
- loc v iloc – Select Rows & Columns in a Dataframe
- Select Rows in a Dataframe based on conditions
- Get unique values in columns of a Dataframe
- Get a list of column and row names in a DataFrame
- Get DataFrame contents as a list of rows.
Use drop() to remove first row of pandas dataframe
In pandas, the dataframe’s drop() function accepts a sequence of row names that it needs to delete from the dataframe. To make sure that it removes the rows only, use argument axis=0 and to make changes in place i.e. in calling dataframe object, pass argument inplace=True.
Checkout complete example to delete the first row 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 row df.drop(index=df.index[0], axis=0, 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 : Name Age City Experience 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12
We fetched the all names of dataframe index as a sequence and passed the first row/index name as the index argument in drop() function, therefore it deleted the first row of dataframe.
Use tail() function to drop first row of pandas dataframe
In python, dataframe provides a function tail(n), it returns the last n rows of dataframe. So, to delete first row of dataframe, just select the last (n-1) rows of dataframe using tail() function, where n is the total rows of dataframe. Then assign these selected rows back to the same variable. It will give an effect that we have deleted first row of the dataframe. For example,
Checkout complete example to remove the first row 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), ('Sam', 56, 'London', 33)] # Create a DataFrame object df = pd.DataFrame( empoyees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) # Delete first row by selecting last n-1 rows df = df.tail(df.shape[0] -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 4 Sam 56 London 33 Modified Dataframe : Name Age City Experience 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12 4 Sam 56 London 33
We fetched the total number of rows in dataframe using df.shape[0] and then passed (df.shape[0] -1) to the tail() function as argument. Therefore it selected the all rows except the first row of dataframe. Then we assigned back all the selected rows of df. So, this is how it deleted the first row of dataframe in place.
Summary:
We learned about different ways to delete the first row 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.