Pandas – Get the First Row of a Dataframe

Selecting rows from a dataframe is now one of the most common tasks anyone can do with pandas. In this tutorial, we will look at how to get the first row of a dataframe in pandas with the help of some examples.

Select the first row of a dataframe

Sometimes you may need to select the first row of a pandas data frame. There are multiple ways to select the first row of a dataframe. For example –

  • Use the pandas dataframe iloc property.
  • Use the pandas head() function.
# Using the iloc[] Property to Retrieve the DataFrame’s First Row
df.iloc[0]

# Retrieve the First Row of All or a Specific Column Using the Head() Function
df.head(1)

Examples

Let’s now look at some examples of using the above syntax.

First, we will create a pandas dataframe that we will be using throughout this tutorial.

import pandas as pd

# employee data
data = {
    "Name": ["Ram", "Sita", "Anita", "Abhinash"],
    "Age": [26, 28, 27, 32],
    "Department": ["Sales", "Sales", "Accounting", "HR"]
}

# create pandas dataframe
df = pd.DataFrame(data)

# display the dataframe
df

Output:

pandas dataframe with employee information

Here, we created a dataframe with data with information on some employees in an office. You can see that the column names in the above dataframe are – “Name”, “Age”, and “Department”.

Example 1: Get the First Row of a Dataframe using the iloc[] property

The Pandas module in Python defines the iloc[] property which allows you to retrieve a specific column or row from the given DataFrame. Using the index values, we can quickly extract any specific value from a column or a row using the iloc[] property.

The following code shows how to get the first row of a dataframe using the iloc[] property.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# Using the iloc[] to Retrieve the DataFrame’s First Row
df.iloc[0]

Output:

Name            Ram
Age              26
Department    Sales
Name: 0, dtype: object

Pandas dataframe rows are indexed starting from 0. Thus, the index of the first row is 0. So, we passed the index value 0 inside the iloc[] property to get the first row of the “df” DataFrame.

You can similarly get the first value (value in the first row) of a specific column in a pandas dataframe using the iloc[] property.

# Retrieve the First Row of Name Column Using the iloc[] property
df['Name'].iloc[0]

Output:

'Ram'

Example 2: Get the First Row of a Dataframe using the head() function

The head() function in pandas retrieves the first “n” rows of a dataframe. The top “n” (the default value is 5) DataFrame’s rows or series are returned using this method. To retrieve just the first row, we pass 1 as an argument to the head() function. The following code shows the same.

# Retrieve the First Row Using the Head() Function
df.head(1)

Output:

the first row of the employee dataframe

By using the value of 1 inside the head function, we retrieved the first row. Just like with the iloc[] property, we can also use the head() function to retrieve the row of a specific column of the DataFrame.

# Retrieve the First Row of Name Column Using the Head() Function
df['Name'].head(1)

Output:

0    Ram
Name: Name, dtype: object

We get the first value in the “Name” column of the above dataframe.

Summary

In this tutorial, we looked at how to access the first row of a pandas dataframe using the following methods –

  • Using the iloc[] property.
  • Using the head() function.

We also looked at how to get the value in the first row for a specific column using the above methods.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

Scroll to Top