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

If you prefer a video tutorial over text, check out the following video detailing the steps in this tutorial –

## How to select the first n rows?

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.

## Examples

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.

### 1. Select first n rows using `head()`

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:

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 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.

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.

### 2. Select first n rows using `iloc`

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.**