Home » How to Get First Row of Pandas DataFrame (With Examples)

How to Get First Row of Pandas DataFrame (With Examples)

by Erma Khan

You can use the following methods to get the first row of a pandas DataFrame:

Method 1: Get First Row of DataFrame

df.iloc[0]

Method 2: Get First Row of DataFrame for Specific Columns

df[['column1', 'column2']].iloc[0]

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

   points  assists  rebounds
0      25        5        11
1      12        7         8
2      15        7        10
3      14        9         6
4      19       12         6
5      23        9         5
6      25        9         9
7      29        4        12

Example 1: Get First Row of Pandas DataFrame

The following code shows how to get the first row of a pandas DataFrame:

#get first row of DataFrame
df.iloc[0]

points      25
assists      5
rebounds    11
Name: 0, dtype: int64

Notice that the values in the first row for each column of the DataFrame are returned.

Example 2: Get First Row of Pandas DataFrame for Specific Columns

The following code shows how to get the values in the first row of the pandas DataFrame for only the points and rebounds columns:

#get first row of values for points and rebounds columns
df[['points', 'rebounds']].iloc[0]

points      25
rebounds    11
Name: 0, dtype: int64

Notice that the values in the first row for the points and rebounds columns are returned.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Get First Column of Pandas DataFrame
How to Add Rows to a Pandas DataFrame
How to Count Number of Rows in Pandas DataFrame

Related Posts