Skip to content
Related Articles

Related Articles

Select first or last N rows in a Dataframe using head() and tail() method in Python-Pandas
  • Last Updated : 02 Jul, 2020

Let’s discuss how to select top or bottom N number of rows from a Dataframe using head() & tail() methods.

1) Select first N Rows from a Dataframe using head() method of Pandas DataFrame :

Pandas head() method is used to return top n (5 by default) rows of a data frame or series

Syntax: Dataframe.head(n).

Parameters: (optional) n is integer value, number of rows to be returned.



Return: Dataframe with top n rows .

Let’s Create a dataframe

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples along with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
                                           'Place', 'College'],
                        index =['a', 'b', 'c', 'd', 'e',
                                'f', 'g', 'i', 'j', 'k'])
  
details

chevron_right


Output:

python-pandas-head-1

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Show first 5 rows of the details dataframe
# from top
details.head()

chevron_right


Output:

python-pandas-head-2

Example 2:



filter_none

edit
close

play_arrow

link
brightness_4
code

# display top 3 rows of the dataframe
details.head(3)

chevron_right


Output:

python-pandas-head-3

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# display top 2 rows of the specific columns
details[['Name', 'Age']].head(2)

chevron_right


Output:

python-pandas-head-4

2) Select last N Rows from a Dataframe using tail() method of Pandas DataFrame :

Pandas tail() method is used to return bottom n (5 by default) rows of a data frame or series.

Syntax: Dataframe.tail(n)

Parameters: (optional) n is integer value, number of rows to be returned.

Return: Dataframe with bottom n rows .

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Show bottom 5 rows of the dataframe
details.tail()

chevron_right


Output:

python-tail-1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Show bottom 3 rows of the dataframe
details.tail(3)

chevron_right


Output:

python-pandas-tail-2

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Show bottom 2 rows of the specific
# columns from dataframe
details[['Name', 'Age']].tail(2)

chevron_right


Output:

python-pandas-tail-3

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :