Pandas: Get last row of dataframe

In this article, we will learn about different ways to select last row of dataframe in pandas.

Select & print last row of dataframe using iloc[]

Before going ahead, let’s first have a look at the dataframe’s iloc attribute.

Overview of dataframe iloc[]

Pandas provides a dataframe attribute iloc[] for location based indexing i.e.

dataframe.iloc[row_section, col_section]
dataframe.iloc[row_section]
  • row_section: The values in it can be,
    • A row number
    • A list of row numbers
    • A range of row numbers – start:end i.e. from start to end-1.
  • column_section: The values in it can be,
    • A column number
    • A column of row numbers
    • A range of column numbers – start:end i.e. from start to end-1.

It selects a part of the dataframe, based on the row & column numbers specified in these row & column sections. If we are interested in only usbset of rows then we can skip the column section, by default it will include all the columns.

Get last row of pandas dataframe as a series

To select the last row of dataframe using iloc[], we can just skip the column section and in row section pass the -1 as row number. Based on negative indexing, it will select the last row of the dataframe,

 df.iloc[-1]

We got the last row of dataframe as a series object.

Get last row of pandas dataframe as a dataframe

If you want the last row of dataframe as a dataframe object then you can provide the range instead of direct number i.e.

df.iloc[-1:]

It will selects the last row of dataframe as a dataframe object.

Complete example:

Let’s see an example, where we will select and print the last row of dataframe using both the specified ways,

import pandas as pd

# List of Tuples
employees = [('Jack',   34, 'Sydney',   5) ,
            ('Mike',   31, 'Delhi' ,   7) ,
            ('Joseph', 16, 'London',   3) ,
            ('David',  41, 'Delhi' ,   4)]

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

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


# Select last row of the dataframe as a series
last_row = df.iloc[-1]

print("last row Of Dataframe: ")
print(last_row)

# Select last row of the dataframe as a dataframe object
last_row_df = df.iloc[-1:]

print("last row Of Dataframe: ")
print(last_row_df)

Output:

Contents of the Dataframe : 
     Name  Age    City  Experience
0    Jack   34  Sydney           5
1    Mike   31   Delhi           7
2  Joseph   16  London           3
3   David   41   Delhi           4

last row Of Dataframe: 
Name          David    
Age              41    
City          Delhi    
Experience        4    
Name: 3, dtype: object 

last row Of Dataframe: 
    Name  Age   City  Experience
3  David   41  Delhi           4

At, last We selected the last row of dataframe as a series object & then printed it. After that we selected the last row as a dataframe as a dataframe and then again printed it.

Select & print last row of dataframe using tail()

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

df.tail(1)

It will return the last row of dataframe as a dataframe object.

Let’s see a complete example,

import pandas as pd

# List of Tuples
employees = [('Jack',   34, 'Sydney',   5) ,
            ('Mike',   31, 'Delhi' ,   7) ,
            ('Joseph', 16, 'London',   3) ,
            ('David',  41, 'Delhi' ,   4)]

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

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

# Select last row of the dataframe 
last_row = df.tail(1)

print("last row Of Dataframe: ")
print(last_row)

Output:

Contents of the Dataframe : 
     Name  Age    City  Experience
0    Jack   34  Sydney           5
1    Mike   31   Delhi           7
2  Joseph   16  London           3
3   David   41   Delhi           4

last row Of Dataframe:
    Name  Age   City  Experience
3  David   41  Delhi           4

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

Get last row of dataframe as list

We can select the last row of dataframe using df.iloc[-1]. It will give us a series object and then by calling the Series’s tolist() function, we can get a list with the contents of last row of dataframe. For example,

import pandas as pd

# List of Tuples
employees = [('Jack',   34, 'Sydney',   5) ,
            ('Mike',   31, 'Delhi' ,   7) ,
            ('Joseph', 16, 'London',   3) ,
            ('David',  41, 'Delhi' ,   4)]

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

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

# Select last row as list
last_row = df.iloc[-1].tolist()

print("last row Of Dataframe: ")
print(last_row)

Output:

Contents of the Dataframe : 
     Name  Age    City  Experience
0    Jack   34  Sydney           5
1    Mike   31   Delhi           7
2  Joseph   16  London           3
3   David   41   Delhi           4

last row Of Dataframe:
['David', 41, 'Delhi', 4]

Summary:

We learned about different ways to get the last row of dataframe.

Advertisements

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