Skip to Content

Pandas – Select first n rows of a DataFrame

In this tutorial, we’ll look at how to select the first n rows of a pandas dataframe.

You can use the pandas dataframe head() function and pass n as a parameter to select the first n rows of a dataframe. Alternatively, you can slice the dataframe using iloc to select the first n rows. The following is the syntax:

# select first n rows using head()
df.head(n)
# select first n rows using iloc
df.iloc[:n,:]

The two methods above return a dataframe with only the first n rows of the original dataframe.

Let’s look at some examples of using the above methods to select first n rows. First, we’ll create a sample dataframe that we’ll be using throughout this tutorial:

import pandas as pd

# dataframe of height and weight football players
df = pd.DataFrame({
    'Height': [167, 175, 170, 186, 190, 188, 158, 169, 183, 180],
    'Weight': [65, 70, 72, 80, 86, 94, 50, 58, 78, 85],
    'Team': ['A', 'A', 'B', 'B', 'B', 'B', 'A', 'A', 'B', 'A']
})

# display the dataframe
print(df)

Output:

   Height  Weight Team
0     167      65    A
1     175      70    A
2     170      72    B
3     186      80    B
4     190      86    B
5     188      94    B
6     158      50    A
7     169      58    A
8     183      78    B
9     180      85    A

The above dataframe contains the height (in cm) and weight (in kg) data of football players from two teams, A and B.

To select the first n rows using the pandas dataframe head() function. Pass n, the number of rows you want to select as a parameter to the function. For example, to select the first 3 rows of the dataframe df:

print(df.head(3))

Output:

   Height  Weight Team
0     167      65    A
1     175      70    A
2     170      72    B

Here, the head() function returned the first three rows of the dataframe df. Note that, by default, the head() function returns the first five rows if n is not specified.

print(df.head())

Output:

   Height  Weight Team
0     167      65    A
1     175      70    A
2     170      72    B
3     186      80    B
4     190      86    B

Fore more on the pandas head() function, refer to its documentation.

You can also select the first n rows of a dataframe by slicing it on index using iloc. For example, to slice the first three rows of the dataframe df:

print(df.iloc[:3,:])

Output:

   Height  Weight Team
0     167      65    A
1     175      70    A
2     170      72    B

Here, we specify the row and column indices we want to select using iloc. Note that, in df.iloc[:3,:] the first slice :3 is used to select all the rows from starting till (but not including) the row with index 3 (that is, rows with index 0, 1, and 2) and the second slice : is used to select all the columns.

For more, refer to pandas’ guide on indexing and selecting data.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


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


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.