How can I retrieve the first row of a Pandas DataFrame?

How can I retrieve the first row of a Pandas DataFrame?

The process of retrieving the first row of a Pandas DataFrame involves using the “iloc” function and specifying the index location of the first row, which is typically 0. This will return a Series object containing the first row of the DataFrame. Alternatively, the “head” function can be used to retrieve the first few rows of a DataFrame, with the default number of rows being 5. Both of these methods allow for easy access and retrieval of the first row of a Pandas DataFrame.

Get First Row of Pandas DataFrame (With Examples)


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.

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

Cite this article

stats writer (2024). How can I retrieve the first row of a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-retrieve-the-first-row-of-a-pandas-dataframe/

stats writer. "How can I retrieve the first row of a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-retrieve-the-first-row-of-a-pandas-dataframe/.

stats writer. "How can I retrieve the first row of a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-retrieve-the-first-row-of-a-pandas-dataframe/.

stats writer (2024) 'How can I retrieve the first row of a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-retrieve-the-first-row-of-a-pandas-dataframe/.

[1] stats writer, "How can I retrieve the first row of a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I retrieve the first row of a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top