How can I get the last row in a Pandas DataFrame? Can you provide an example?

How can I get the last row in a Pandas DataFrame? Can you provide an example?

To retrieve the last row in a Pandas DataFrame, one can use the iloc method with the index value of -1. This will return the last row in the DataFrame. For example, if we have a DataFrame named “df”, we can use the code “df.iloc[-1]” to access the last row. This will allow us to retrieve and manipulate the data in the last row of the DataFrame.

Get Last Row in Pandas DataFrame (With Example)


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.

The following tutorials explain how to perform other common tasks in pandas:

Cite this article

stats writer (2024). How can I get the last row in a Pandas DataFrame? Can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-get-the-last-row-in-a-pandas-dataframe-can-you-provide-an-example/

stats writer. "How can I get the last row in a Pandas DataFrame? Can you provide an example?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-get-the-last-row-in-a-pandas-dataframe-can-you-provide-an-example/.

stats writer. "How can I get the last row in a Pandas DataFrame? Can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-get-the-last-row-in-a-pandas-dataframe-can-you-provide-an-example/.

stats writer (2024) 'How can I get the last row in a Pandas DataFrame? Can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-get-the-last-row-in-a-pandas-dataframe-can-you-provide-an-example/.

[1] stats writer, "How can I get the last row in a Pandas DataFrame? Can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I get the last row in a Pandas DataFrame? Can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top