How to Get last N rows of Pandas Dataframe

In this post, we are going to learn about how to Get last N rows of Pandas Dataframe. We will use Pandas dataframe tail() and the iloc[] function and slice to get the last row of Pandas data frame. To select the last N or bottom rows of Pandas data frame we slice the data frame. We can slice the data frame by rows and columns by passing the corresponding index in the iloc[] attribute of the data frame.

1. How to Get last N rows of Pandas Dataframe using iloc[]


To get the last N rows of Pandas data frame, We will use negative row indexing using the iloc[-n] attribute, where N is an integer to select the last row of Pandas data frame.

Syntax

dataframe.iloc[Row_index, col_Index]
dataframe.iloc[row_Index]

Parameters

  • Row_index: It can be a row index, list of row indices, or range of rows indices [startIndex :endindex-1]
  • col_Index: It can be a column index, list of columns indices, or range of columns indices [startIndex :endindex-1].
import pandas as pd

     
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[97,98,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}

dfobj = pd.DataFrame(data)

print('Last 1 row of Pandas dataframe:\n',dfobj.iloc[-1:]) 

Output

  Last 1 rows of Pandas dataframe
   Name  Marks Subject
3  David    100     Phy

2. How to Get last N rows of last N columns


In this example, we will understand how to Get last N rows of Pandas Dataframe. Sometimes instead of displaying all columns of the last n rows of Pandas data frame, We have need to display the last columns of the data frame’s last rows. In this Python example, we are selecting the last 2 rows of the last two columns of Pandas dataframe by using the dataframe .iloc[] attribute.

#pytho3.x program  to Get last N rows of Pandas Dataframe
import pandas as pd

data = {
    'Name': ['Jack', 'Jack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}

dfobj = pd.DataFrame(data)


resdf = dfobj.iloc[-2:,-2:]
print(resdf)  

Output

   Marks Subject
2    100    Math
3    100     Phy

3. How to get last range of rows of Pandas data frame using slice


In this Python program example, We are selecting the range of rows from Pandas data frame by using slicing. The start index is 2 and end index is 3, So it will get only the row at index 2. In slicing the endindex is always endindex-1.

import pandas as pd

data = {
    'Name': ['Jack', 'Jack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}

dfobj = pd.DataFrame(data)

print('The last second row of Pandas dataframe:\n',dfobj[2:3])

Output

The last second row of Pandas dataframe  :
   Name  Marks Subject
2  Max    100    Math

4. How to get last N rows of Pandas Dataframe using tail()


The Pandas data frame tail() function takes an optional parameter N (number of rows) and returns the last N rows from the data frame based on position. By default, it returns the last 5 rows, when the value of N is not passed to the method.

import pandas as pd

data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[97,98,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}

dfobj = pd.DataFrame(data)

print(dfobj.tail(2))

Output

    Name  Marks Subject
2    Max    100    Math
3  David    100     Phy

5. How to get last N rows of specific columns


In this example, we have used the df.tail() function to get the last n rows of specific columns. In below code will select last 2 rows of columns ‘Name’ and ‘Marks’.

import pandas as pd

data = {
    'Name': ['Jack', 'Jack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}

dfobj = pd.DataFrame(data)

print(dfobj[['Name', 'Marks']].tail(2))

Output

  Name  Marks
2    Max    100
3  David    100

Summary

In this post, we have learned how to Get the last N rows of Pandas Dataframe by using iloc[], slicing, and tail() with examples. We have also covered getting the last n columns of the last N rows.