How to drop the first row in Pandas

In this article, you will learn how to drop the first row in Pandas.

A Pandas DataFrame is nothing but a two-dimensional data structure or two-dimensional array that represents the data in rows and columns. In other words, it is compared to rectangular grids used to store data. It is


This content originally appeared on CodeSource.io - Quality Web & programming Tutorials and was authored by Codesource Staff

How to drop the first row in Pandas

In this article, you will learn how to drop the first row in Pandas.

A Pandas DataFrame is nothing but a two-dimensional data structure or two-dimensional array that represents the data in rows and columns. In other words, it is compared to rectangular grids used to store data. It is open-source and potent, fast, and easy to use. Basically, while working with big data we need to analyze, manipulate and update them and the pandas’ library plays a lead role there. Sometimes, we may need to drop the first row in Pandas. There are various ways to perform this action. Such as – using the iloc(), drop(), or tail() function. In this article, we will explore these functions and see how we can perform this action in Pandas DataFrame. First, let’s create a simple Pandas DataFrame in the below section:

import pandas as pd

student_df = pd.DataFrame({'Name' : ['Alex', 'Deven', 'John', 'Roy', 'Rohit'],
                           'Age' : [21, 23, 19, 22, 20],
                           'Ct_marks1' : [77, 84, 90, 67, 55],
                           'Ct_marks2' : [58, 74, 77, 87, 75]
                          })

print(student_df)

# Output: 
#     Name  Age  Ct_marks1  Ct_marks2
# 0   Alex   21         77         58
# 1  Deven   23         84         74
# 2   John   19         90         77
# 3    Roy   22         67         87
# 4  Rohit   20         55         75

Here, you can see that we have created a simple Pandas DataFrame that represents a few columns. We will perform this action in this Pandas DataFrame.

Example One: drop the first row in Pandas using iloc()

import pandas as pd
import datetime as dt

student_df = pd.DataFrame({'Name' : ['Alex', 'Deven', 'John', 'Roy', 'Rohit'],
                           'Age' : [21, 23, 19, 22, 20],
                           'Ct_marks1' : [77, 84, 90, 67, 55],
                           'Ct_marks2' : [58, 74, 77, 87, 75]
                          })
student_df = student_df.iloc[1:, : ]
print(student_df)

# Output: 
#     Name  Age  Ct_marks1  Ct_marks2
# 1  Deven   23         84         74
# 2   John   19         90         77
# 3    Roy   22         67         87
# 4  Rohit   20         55         75

Here, you can see that we use the iloc() to drop the first row from the DataFrame. We start printing rows from 1 and it simply ignores the first one.

Example Two: drop the first row in Pandas using drop()

import pandas as pd
import datetime as dt

student_df = pd.DataFrame({'Name' : ['Alex', 'Deven', 'John', 'Roy', 'Rohit'],
                           'Age' : [21, 23, 19, 22, 20],
                           'Ct_marks1' : [77, 84, 90, 67, 55],
                           'Ct_marks2' : [58, 74, 77, 87, 75]
                          })
student_df = student_df.drop(index = 0)
print(student_df)

# Output: 
#     Name  Age  Ct_marks1  Ct_marks2
# 1  Deven   23         84         74
# 2   John   19         90         77
# 3    Roy   22         67         87
# 4  Rohit   20         55         75

The Drop() function behaves the opposite of the iloc(). Here, we mention the first row and this function simply removes the first one.

Example Three: drop the first row in Pandas using tail()

import pandas as pd

student_df = pd.DataFrame({'Name' : ['Alex', 'Deven', 'John', 'Roy', 'Rohit'],
                           'Age' : [21, 23, 19, 22, 20],
                           'Ct_marks1' : [77, 84, 90, 67, 55],
                           'Ct_marks2' : [58, 74, 77, 87, 75]
                          })
student_df = student_df.tail(student_df.shape[0]-1)
print(student_df)

# Output: 
#     Name  Age  Ct_marks1  Ct_marks2
# 1  Deven   23         84         74
# 2   John   19         90         77
# 3    Roy   22         67         87
# 4  Rohit   20         55         75

Like the iloc() and drop() function there is another way of performing this action. The tail() function performs exactly the other two functions. You can see that we use the tail() function along with the shape() to ignore the first row. In the output, you can see that this function removes the first row. These are some useful approaches to perform this action in Pandas.


This content originally appeared on CodeSource.io - Quality Web & programming Tutorials and was authored by Codesource Staff


Print Share Comment Cite Upload Translate Updates
APA

Codesource Staff | Sciencx (2022-10-02T14:34:23+00:00) How to drop the first row in Pandas. Retrieved from https://www.scien.cx/2022/10/02/how-to-drop-the-first-row-in-pandas-2/

MLA
" » How to drop the first row in Pandas." Codesource Staff | Sciencx - Sunday October 2, 2022, https://www.scien.cx/2022/10/02/how-to-drop-the-first-row-in-pandas-2/
HARVARD
Codesource Staff | Sciencx Sunday October 2, 2022 » How to drop the first row in Pandas., viewed ,<https://www.scien.cx/2022/10/02/how-to-drop-the-first-row-in-pandas-2/>
VANCOUVER
Codesource Staff | Sciencx - » How to drop the first row in Pandas. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/02/how-to-drop-the-first-row-in-pandas-2/
CHICAGO
" » How to drop the first row in Pandas." Codesource Staff | Sciencx - Accessed . https://www.scien.cx/2022/10/02/how-to-drop-the-first-row-in-pandas-2/
IEEE
" » How to drop the first row in Pandas." Codesource Staff | Sciencx [Online]. Available: https://www.scien.cx/2022/10/02/how-to-drop-the-first-row-in-pandas-2/. [Accessed: ]
rf:citation
» How to drop the first row in Pandas | Codesource Staff | Sciencx | https://www.scien.cx/2022/10/02/how-to-drop-the-first-row-in-pandas-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.