How to Get Last Row in Pandas DataFrame (With Example)

Getting the last row in a Pandas DataFrame is as simple as using the iloc functionality. You can use the [-1] index to access the last row of the DataFrame, which can be useful when you want to access the last entry of a given dataset. This can be especially useful when combined with other features of the Pandas library such as sorting or slicing. An example of this would be df.iloc[-1] to access the last row of the DataFrame.


You can use the following methods to get the last row in a pandas DataFrame:

Method 1: Get Last Row (as a Pandas Series)

last_row = df.iloc[-1]

Method 2: Get Last Row (as a Pandas DataFrame)

last_row = df.iloc[-1:]

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'assists': [3, 4, 4, 5, 6, 7, 8, 12, 15, 11],
                   'rebounds': [1, 3, 3, 5, 2, 2, 1, 1, 0, 14],
                   'points': [20, 22, 24, 25, 20, 28, 15, 29, 11, 12]})

#view DataFrame
print(df)

   assists  rebounds  points
0        3         1      20
1        4         3      22
2        4         3      24
3        5         5      25
4        6         2      20
5        7         2      28
6        8         1      15
7       12         1      29
8       15         0      11
9       11        14      12

Example 1: Get Last Row (as a Pandas Series)

The following code shows how to get the last row of the DataFrame as a pandas Series:

#get last row in Data Frame as Series
last_row = df.iloc[-1]

#view last row
print(last_row)

assists     11
rebounds    14
points      12
Name: 9, dtype: int64

We can use the type() function to confirm that the result is indeed a pandas Series:

#view type
type(last_row)

pandas.core.series.Series

The result is indeed a pandas Series.

Example 2: Get Last Row (as a Pandas DataFrame)

The following code shows how to get the last row of the DataFrame as a pandas DataFrame:

#get last row in Data Frame as DataFrame
last_row = df.iloc[-1:]

#view last row
print(last_row)

   assists  rebounds  points
9       11        14      12

We can use the type() function to confirm that the result is indeed a pandas DataFrame:

#view type
type(last_row)

pandas.core.frame.DataFrame

The result is indeed a pandas DataFrame.

x