Как использовать функцию Pandas head() (с примерами)
Вы можете использовать функцию head() для просмотра первых n строк кадра данных pandas.
Эта функция использует следующий базовый синтаксис:
В следующих примерах показано, как использовать этот синтаксис на практике со следующими пандами DataFrame:
Пример 1: просмотр первых 5 строк DataFrame
По умолчанию функция head() отображает первые пять строк DataFrame:
Пример 2: просмотр первых n строк DataFrame
Мы можем использовать аргумент n для просмотра первых n строк кадра данных pandas:
Пример 3: просмотр первых n строк определенного столбца
В следующем коде показано, как просмотреть первые пять строк определенного столбца в DataFrame:
Пример 4. Просмотр первых n строк нескольких столбцов
В следующем коде показано, как просмотреть первые пять строк нескольких определенных столбцов в DataFrame:
Дополнительные ресурсы
В следующих руководствах объясняется, как выполнять другие распространенные функции в pandas:
Exploratory Data Analysis using Python Pandas: A Tutorial
![]()
In this tutorial, we will learn about exploratory data analysis using Python Pandas. In exploratory data analysis, we analyze the input dataset to summarize its main characteristics. Sometimes, we examine the main features of the input dataset visually using different standard plots.
This is a beginner-friendly tutorial. Here, we assume that the readers are familiar…

Written by Dr. Soumen Atta, Ph.D.
Assistant Professor, Center for Information Technologies and Applied Mathematics, School of Engineering and Management, University of Nova Gorica, Slovenia
How to Use the Pandas Head Method
This tutorial will show you how to use the Pandas head technique to get the first few rows of a Pandas dataframe.
Here, I’ll explain what the technique does, explain the syntax, and show you a few clear examples of how to use it.
If you need something specific, you can click on any of these following links, and it will take you to the appropriate section in the tutorial.
Table of Contents:
A Quick Introduction to the Pandas ‘head’ Technique
So what does the head() method do?
The Pandas head() method is very simple.
The head() method returns the first few rows of a Pandas dataframe.
To help you understand that though, I’ll explain a few things about Pandas and give you a quick refresher on Pandas dataframes.
Pandas is a Data Manipulation Toolkit for Python
Let’s quickly review Pandas, since understanding Pandas is important for understanding the Pandas head technique.
Pandas is a data manipulation package for the Python programming language.
We use Pandas tools to “wrangle” or manipulate data.
But specifically, we typically use Pandas to work with data that exists within a particular data structures.
Pandas works with Dataframes and Structured Data
Pandas has two primary data structures. There’s the Pandas Dataframe and there’s the Pandas Series object. (In this tutorial, we’ll mostly be talking about the Pandas dataframe.)
Pandas dataframes have a row-and-column structure.

In this sense, dataframes are similar to Excel spreadsheets. They have rows of data (i.e., data records) and columns (i.e., variables).
Pandas has Tools for Working with Dataframes
Pandas has several tools for creating these dataframes, but also tools for inspecting, reshaping, and manipulating these dataframes.
For example, you can use the Pandas DataFrame() function to create a dataframe from raw data.
You can use the Pandas assign method to add new variables to a dataframe.
And there are several tools to inspect the contents of a dataframe.
That’s where the Pandas head() method comes in.
The Pandas ‘head’ method retrieves the first few rows of data
The Pandas head() method is very simple. It retrieves the first few rows of data from a Pandas dataframe.

The syntax of Pandas dataframe.head
Here, I’ll walk you through the syntax of the Pandas head() method.
Keep in mind that the explanation that follows assumes that you already have a Pandas dataframe that you’re working with. You can see an example of how to create a Pandas dataframe in the examples section.
dataframe.head syntax
Ok. Let’s look at the syntax for the dataframe.head method.
Essentially, you start by typing the name of your dataframe. Here, for the sake of simplicity, we’ll assume that the dataframe is called your_dataframe .

Examples: How to get the first few rows of a Pandas dataframe
Here, I’ll show you a few examples of how to use the Pandas head() method.
You can click on either of the links, and it will take you to the appropriate example.
Examples:
Run this code first
Before you run the examples, you actually need to do two things: you need to import Pandas, and you need to create the dataframe that we’ll be working with.
Import Pandas
First, you’ll simply need to import Pandas.
If you’re reading this blog post, you’ve probably already done this and know how to do it …
But just in case, you can import Pandas with the following code:
Once we do this, we’ll be able to call Pandas functions with the prefix ‘ pd ‘.
Create Dataframe
Next, we’ll create the dataframe that we’re going to work with.
To do this, we’re going to use the Pandas DataFrame() method to create a dataset called sales_data .
And let’s print it out so you can see the contents.
As you can see, sales_data contains 11 rows of data with information about revenues (i.e., sales) and expenses.
EXAMPLE 1: How to return the first 5 rows of data (default)
Now that we have our data, let’s use the head() method to retrieve the first few rows of data.
Here, we’re going to use the head() method with the default settings.
Let’s take a look.
Explanation
Here, we called the head() method with the default settings. That is, we didn’t use any parameters.
By default, the n parameter is set to n = 5 , so by default, the head() method has retrieved the first 5 rows.
Notice also that we called the method just like we call all methods in Python. We typed the name of the object ( sales_data ), then a ‘dot’, and then the name of the method, head() .
This is as simple as it gets.
EXAMPLE 2: How to specify a specific number of rows
For the second and final example, we’ll modify things a little bit.
In the previous example, we used the default settings, which caused the head() method to retrieve 5 rows of data.
Here, we’ll change that slightly.
We’ll use the n parameter so that the head method returns only 3 rows.
Let’s take a look.
Explanation
This is actually very simple.
Here, we set the n parameter to n = 3 . This caused Pandas head() to return the first 3 rows of data.
Obviously, we can set the argument to almost any value we want. For example, if we had a larger dataset, we could set n = 10 , and it would return 10 rows. We could set n = 20 , and it would return 20 rows. And so on.
Having said that, since we often use the Pandas head method for simple data inspection, we’ll typically set n to somewhere between 3 and 10.
Typically, when we use this technique, we just want to get a quick view of what the data looks like. If we set n to a large value, it can sometimes show too much data, and can make the output hard to understand at a glance.
You can experiment though and try different values. Different datasets sometimes need more or less data inspection.
Leave your other questions in the comments below
Do you have questions about the Pandas head technique?
Is there something we didn’t cover?
If so, leave your questions in the comments section near the bottom of the page.
If you want to master Pandas, join our course
In this tutorial, I’ve explained how to use the head method, but we’ve really just scratched the surface of what Pandas can do. If you want to master data wrangling in Pandas, you’ll need to learn a lot more.
That said, if you’re serious about learning and mastering Pandas, you should join our premium online course, Pandas Mastery.
Pandas Mastery is our online course that will teach you everything you need to know about data manipulation with the Pandas package.
Inside the course, you’ll learn all of the essential techniques, like:
- how to create dataframes
- inspecting dataframes
- subsetting dataframes
- filtering data by logical conditions
- adding new variables
- reshaping data
- working with Pandas indexes
- and much more …
Additionally, you’ll discover our unique practice system that will enable you to memorize all of the syntax you learn. If you practice like we show you, you’ll memorize the syntax in only a few weeks!
Pandas DataFrame head, tail, at, iat
In this article, we learn how to use DataFrame.head() and DataFrame.tail() functions to select top and bottom rows of the Pandas DataFrame respectively. Also, learn DataFrame.at() and DataFrame.iat() functions to access a specific value in the DataFrame.
Table of contents
How to use DataFrame.head() function
This function is used to see the first n rows in the DataFrame. It is beneficial when we have massive datasets, and it is not possible to see the entire dataset at once.
It takes input as the number of rows to be displayed from the top. The default value is 5.
Syntax
Example
In the below Student DataFrame with columns like Name, Age, and Marks. If we apply DataFrame.head() function, we can see that only the first five rows are displayed.

Output
Select top n rows in pandas DataFrame
When we want to see a smaller section of data, we can use the function DataFrame.head() and pass a parameter as the number of rows to display from the top.
Example
In the below example, after we apply the DataFrame.head(3) function, only the first three rows of the DataFrame are displayed.
Output
Select top rows except for last n rows
When we have a vast DataFrame, and we want to see all the rows except for the last n rows, we can pass the negative value as a parameter to DataFrame.head() .
Example
In the below example, if we want to display all the rows except the bottom two rows, we can use DataFrame.head(-2) function.
Output
Select top rows from multi-index DataFrames
When Python pandas DataFrame has multiple row index or column headers, then are called multi-level or hierarchical DataFrame. As we have discussed in the above section, we can use the DataFrame.head() function on multi-index DataFrames to display the top rows.
The below diagram shows hierarchical DataFrame of Student data with two-column headers where column labels ‘Name‘ and ‘Marks‘ are at level 0 and ‘Surname‘ and ‘Percentage‘ at level 1. Similarly, two-row indexes are index ‘Standard‘ at level 0 and ‘Class‘ at level 1 of the DataFrame.
Example
The below example shows how to create such DataFrame and display top rows rather than the whole DataFrame.

Output
How to use DataFrame.tail() function
We can use the DataFrame.tail() function to display the last n rows of the DataFrame. Like the head function, this function is used when we want to view a smaller section of the entire DataFrame.
It takes input as the number of rows to be displayed from the bottom. The default value is 5.
Syntax
Example
In the below Student DataFrame with columns like Name, Age, and Marks. If we apply DataFrame.tail() function, we can see that only the bottom five rows are displayed in the output.

Output
Select bottom n rows in pandas DataFrame
When we want to see a smaller section of data from the bottom of the DataFrame, we can use the function DataFrame.tail() and pass a parameter as the number of rows to display from the bottom.
Example
In the below example, after we apply the DataFrame.tail(3) function, we see that only the last 3 rows of the DataFrame are displayed.
Output
Select bottom rows except for first n rows
When we want to see our entire dataset except for the first few rows, we can use DataFrame.tail() function and pass the negative value as a parameter to it.
Example
In the below example, if we display all the rows except the top 2 rows using DataFrame.tail(2) .
Output
Select bottom rows from the multi index DataFrame
We can apply the DataFrame.tail() function on multi-index DataFrames as well. It works in the same way as normal DataFrames.
Example
In our example, after we have applied the DataFrame.tail() function, only the bottom 5 row are displayed.

Output
Select value using row and column labels using DataFrame.at
There are cases in the field of Data Science that we need to access a specific element of the DataFrame using its column label and row index. In such cases, we can use the DataFrame.at property and pass the row index and column labels of the value to access as parameters. This property can be used with Multi-index DataFrame as well.
Note: It throws KeyError if the label does not exist in DataFrame.
Example
In the below example, after we use the DataFrame.at[2,»Age»] function, we get 19 as the output because 19 is the value present at row 2 and column Age.

Set specific value in pandas DataFrame
When we want to update the value of the particular element from DataFrame based on its column label and row index, we can use DataFrame.at property.
Example
In the below example, after we have applied DataFrame.at[2,»Age»]=50 on our DataFrame, the value at that position changed from 19 to 50.
Output
Select value using row and column position using DataFrame.iat
We want to access a specific element from a very large DataFrame, but we do not know its column label or row index. We can still access such an element using its column and row positions. For that, we can use DataFrame.iat property of python pandas. Unlike DataFrame.at it can work on the row and column index position of the DataFrame.
Note: Index positions starts at 0.
Example
In the below example, we access the second row and the third column using DataFrame.iat[1,2] .

Set specific value in pandas DataFrame
When we want to update the value of the particular element from DataFrame based on its column and row position, we can use DataFrame.iat property.
Example
In the below example, we change the value at the second row and the third column using DataFrame.iat[1,2]=90 .
Output
Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.
About Vishal
Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!
Related Tutorial Topics:
Python Exercises and Quizzes
Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.