Head and tail function in Python pandas (Get First N Rows & Last N Rows)

In this tutorial we will learn how to get the snap shot of the data, by getting first few rows and last few rows of the data frame i.e Head and Tail function in python. Head function returns first n rows and tail function return last n rows

We will use the iris data set for demonstration of head and tail function in python

Loading the iris data set:

# load iris data set from sklearn

import pandas as pd
from sklearn import datasets
iris=pd.DataFrame(datasets.load_iris().data)

 

Head Function in Python (Get First N Rows):

# head function in python

iris.head()

head function with no arguments gets the first five rows of data from the data frame so the output will be

head function in python 1

# head function in python with arguments

iris.head(8)

head function with specified N arguments, gets the first N rows of data from the data frame so the output will be

head function in python 2

 

Tail Function in Python (Get Last N Rows):

# Tail function in python

iris.tail()

tail function with no arguments gets the last five rows of data from the data frame so the output will be

tail function in python 1

# tail function in python with arguments

iris.tail(8)

tail function with specified N arguments, gets the last N rows of data from the data frame so the output will be

tail function in python 2

previous head and tail function in python                                                                                                                next head and tail function in python