Pandas: Get last N rows of dataframe

In this article, we will learn about different ways to get the last N rows of dataframe in pandas.

Get last N rows of dataframe in pandas using iloc[]

Before jumping into the solution, let’s last have a brief view of the dataframe’s iloc.

Overview of dataframe iloc[]

In Pandas, the dataframe class has an attribute iloc[] for location based indexing i.e.

dataframe.iloc[row_section, col_section]
dataframe.iloc[row_section]
  • row_section: It can be,
    • A row number
    • A list of row numbers
    • A range of row numbers like start:end i.e. inlcude rows from number start to end-1.
  • column_section: It can be
    • A column number
    • A column of row numbers
    • A range of column numbers like start:end i.e. inlcude column from number start to end-1.

It selects a slice of the dataframe based on the row numbers & column numbers provided in these row & column sections. If you to include all columns and focus just on selecting few rows, then you can skip the column section.

Get last N rows of pandas dataframe

To select the last n rows of the dataframe using iloc[], we can skip the column section and in row section pass a range of column numbers i.e. -N to end. It will select the last N rows,

df.iloc[-N:]

Here, we used the negative indexing i.e. we started from -N, which is nth row from last and then we went till the end. Therefore it selected only the last N rows of the dataframe.

Complete example

Let’s see an example, where we will select and print the last 3 rows of a dataframe using iloc[],

import pandas as pd

# List of Tuples
employees = [('Jack',    34, 'Sydney',   5),
            ('Shaun',   31, 'Delhi' ,   7),
            ('Meera',   29, 'Tokyo' ,   3),
            ('Mark',    33, 'London' ,  9),
            ('Shachin', 16, 'London',   3),
            ('Eva',     41, 'Delhi' ,   4)]

# Create a DataFrame object
df = pd.DataFrame(  employees, 
                    columns=['Name', 'Age', 'City', 'Experience'])

print("Contents of the Dataframe : ")
print(df)

N = 3
# Select last N rows of the dataframe as a dataframe object
last_n_rows = df.iloc[-N:]

print("last N rows Of Dataframe: ")
print(last_n_rows)

Output:

Contents of the Dataframe : 
      Name  Age    City  Experience
0     Jack   34  Sydney           5
1    Shaun   31   Delhi           7
2    Meera   29   Tokyo           3
3     Mark   33  London           9
4  Shachin   16  London           3
5      Eva   41   Delhi           4

last N rows Of Dataframe: 
      Name  Age    City  Experience
3     Mark   33  London           9
4  Shachin   16  London           3
5      Eva   41   Delhi           4

We selected the last three rows of the dataframe as a dataframe and printed it.

Get last N rows of a dataframe using tail()

In Pandas, the dataframe provides a function tail(n). It returns the last N rows of dataframe. We can use it to get only the last N row of the dataframe,

df.tail(N)

It will return the last N rows of dataframe as a dataframe object.

Let’s see a complete example,

import pandas as pd

# List of Tuples
employees = [('Jack',    34, 'Sydney',   5),
            ('Shaun',   31, 'Delhi' ,   7),
            ('Meera',   29, 'Tokyo' ,   3),
            ('Mark',    33, 'London' ,  9),
            ('Shachin', 16, 'London',   3),
            ('Eva',     41, 'Delhi' ,   4)]

# Create a DataFrame object
df = pd.DataFrame(  employees, 
                    columns=['Name', 'Age', 'City', 'Experience'])

print("Contents of the Dataframe : ")
print(df)

N = 3
# Select last N rows of the dataframe 
last_n_rows = df.tail(N)

print("last N rows Of Dataframe: ")
print(last_n_rows)

Output:

Contents of the Dataframe : 
      Name  Age    City  Experience
0     Jack   34  Sydney           5
1    Shaun   31   Delhi           7
2    Meera   29   Tokyo           3
3     Mark   33  London           9
4  Shachin   16  London           3
5      Eva   41   Delhi           4

last N rows Of Dataframe:
      Name  Age    City  Experience
3     Mark   33  London           9
4  Shachin   16  London           3
5      Eva   41   Delhi           4

Using the tail() function, we fetched the last 3 rows of dataframe as a dataframe and then just printed it.

Get last N rows of dataframe with specific columns

Suppose we are want the last 3 rows of dataframe but it should include only 2 specified columns. lets see how to do that,

import pandas as pd

# List of Tuples
employees = [('Jack',    34, 'Sydney',   5),
            ('Shaun',   31, 'Delhi' ,   7),
            ('Meera',   29, 'Tokyo' ,   3),
            ('Mark',    33, 'London' ,  9),
            ('Shachin', 16, 'London',   3),
            ('Eva',     41, 'Delhi' ,   4)]

# Create a DataFrame object
df = pd.DataFrame(  employees, 
                    columns=['Name', 'Age', 'City', 'Experience'])

print("Contents of the Dataframe : ")
print(df)

N = 3
# Select last N rows of the dataframe 
last_n_rows = df[['Name', 'City']].tail(N)

print("last N rows Of Dataframe: ")
print(last_n_rows)

Output:

Contents of the Dataframe : 
      Name  Age    City  Experience
0     Jack   34  Sydney           5
1    Shaun   31   Delhi           7
2    Meera   29   Tokyo           3
3     Mark   33  London           9
4  Shachin   16  London           3
5      Eva   41   Delhi           4

last N rows Of Dataframe: 
      Name    City
3     Mark  London
4  Shachin  London
5      Eva   Delhi

We last selected two columns of the dataframe i.e. Name & City as a dataframe object and then we called the tail(3) function on that to select last 3 enteries of that dataframe.

Summary:

We learned about different ways to get the last N rows of dataframe in pandas.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top