How to Drop First N Rows Pandas DataFrame in Python?

How to Drop First Two Rows Pandas DataFrame in Python

Are you struggling with how to drop the first N (Where N = 1, 2, 3, …) rows of Pandas DataFrame in Python, or do you want to drop multiple rows in Pandas DataFrame?

Pandas DataFrames are the most suitable data structures to manage and analyze large datasets. A DataFrame is a two-dimensional size-mutable tabular data structure with labeled axes, columns, and rows. It has three essential components rows, columns, and data.

In this article, we’ll discuss how to drop the first N or two rows of Pandas or multiple rows based on some specific indexes in Python. Python is a versatile programming language that provides numerous solutions to a single problem. Similarly to drop rows in Python, we have three different solutions to it.

 

 

How to Drop The First N Rows of Pandas DataFrame in Python?

Dropping some specific rows or whole columns is a frequent task when analyzing large data sets to get accurate results. It is needed sometimes when we’ve missing or invalid entries in the dataset. Basically, it is done in the data cleansing process of data analysis to make to data precise and concise and get more accurate results.

In Python, we’ve three different approaches to dropping some specific rows or columns. They are the following as;

  1. The iloc attribute to drop the first two rows of Pandas DataFrame
  2. The drop() function to drop rows of a DataFrame in Python
  3. The tail() function to drop rows of a DataFrame in Python

Let’s discuss each along with code examples 👇

 

Method 1: Use iloc to Drop The First Two Rows of Pandas DataFrame in Python

In Python, the Pandas DataFrame provides an attribute iloc, to select a set of rows and columns using position-based indexing.

 

Let’s see the syntax of iloc.

df = df.iloc[start_row_index : end_row_index , start_col_index : end_col_index ]

 

 Let’s see a practical example.

 

Code

#import libraries

import pandas as pd



# create a DataFrame

df = pd.DataFrame({'Name': ["Mark", "Elon", "Bill", "Jeff"],

                  'Age': [38, 51,67,58],

                  'Company': ['Meta', 'Tesla', 'Microsoft', 'Amazon']                  

                  })



print("Orignal DataFrame")

print(df)



# Drop the first two rows using 'iloc' and assign the rest to 'df'

df = df.iloc[2: , :]



print("\n\nDataFrame after dropping the first two rows")

print(df)

 

Output

Original DataFrame

     Name Age Company

0   Mark   38   Meta

1   Elon    51   Tesla

2   Bill      67   Microsoft

3   Jeff     58   Amazon



DataFrame after dropping the first two rows

   Name Age  Company

2  Bill     67    Microsoft

3  Jeff    58    Amazon

 

In the above code, the statement df.iloc[2:, :] is responsible for dropping the first two rows of the DataFrame. It says that print the DataFrame from index 2 onwards, whereas the rows at index 0 and 1 are dropped.

 

 

Method 2: Use The drop() Function to Drop The First Two Rows of a Pandas DataFrame

In Pandas, DataFrame, the drop() function is used to drop a sequence of rows from the DataFrame. And make sure that the inplace = ture so the changes are saved into the new DataFrame. 

Let’s see a practical example of dropping the first two rows of a DataFrame in Python using the drop() function:

 

Code

#import libraries

import pandas as pd



#create a DataFrame

df = pd.DataFrame({'Name': ["Mark", "Elon", "Bill", "Jeff"],

                  'Age': [38, 51,67,58],

                  'Company': ['Meta', 'Tesla', 'Microsoft', 'Amazon']                  

                  })



print("Orignal DataFrame")

print(df)



# Drop the first two rows using the 'drop' function

df.drop(index=df.index[0:2], inplace=True)



print("\n\nDataFrame after dropping the first two rows")

print(df)

 

Output

Original DataFrame

     Name Age Company

0   Mark   38   Meta

1   Elon    51   Tesla

2   Bill      67   Microsoft

3   Jeff     58   Amazon




DataFrame after dropping the first two rows

   Name Age  Company

2  Bill     67    Microsoft

3  Jeff    58    Amazon

 

 

Method 3: Use The tail() Function to Drop The First Two Rows of a Pandas Data Frame

In Python, DataFrame, we have a tail() function used to drop rows from a DataFrame. And to drop the first row of a DataFrame, we’ll select the last (n-1) rows of the DataFrame using the tail() function, whereas the total number of rows is n. But since, in this case, we are concerned with dropping the first two rows of a DataFrame, we’ll use -2.

Let’s see a practical example. 

 

Code

#import libraries

import pandas as pd



# create a DataFrame

df = pd.DataFrame({'Name': ["Mark", "Elon", "Bill", "Jeff"],

                  'Age': [38, 51,67,58],

                  'Company': ['Meta', 'Tesla', 'Microsoft', 'Amazon']                  

                  })



print("Orignal DataFrame")

print(df)



# Delete the first two rows of DataFrame using the 'tail()"

df = df.tail(df.shape[0] -2)



print("\n\nDataFrame after dropping the first two rows")

print(df)

 

Output

Original DataFrame

     Name Age Company

0   Mark   38   Meta

1   Elon    51   Tesla

2   Bill      67   Microsoft

3   Jeff     58   Amazon




DataFrame after dropping the first two rows

   Name Age  Company

2  Bill     67    Microsoft

3  Jeff    58    Amazon

 

 

Conclusion

To summarize this article on how to drop the first N or two rows of a pandas DataFrame, we’ve discussed how to drop rows from a data frame using three different approaches in Python, along with code examples.

The following are the three different approaches we’ve discussed in this article;

  1. The iloc attribute of Pandas DataFrame.
  2. The drop() function.
  3. The tail() function.

I hope you’ve found this article helpful ✨ if yes, don’t forget to share. 

To learn more 💡, here is a quick question for you. How to drop the last two rows of a DataFrame using the drop() function?

 

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share