Python - How to access the last element in a Pandas series?

PythonServer Side ProgrammingProgramming

We will be using the iat attribute to access the last element, since it is used to access a single value for a row/column pair by integer position.

Let us first import the required Pandas library −

import pandas as pd

Create a Pandas series with numbers −

data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100])

Now, get the last element using iat() −

data.iat[-1]

Example

Following is the code −

import pandas as pd

# pandas series
data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100])

print"Series...\n",data

# get the first element
print"The first element in the series = ", data.iat[0]

# get the last element
print"The last element in the series = ", data.iat[-1]

Output

This will produce the following output −

Series...
0     10
1     20
2      5
3     65
4     75
5     85
6     30
7    100
dtype: int64
The first element in the series = 10
The last element in the series = 100
raja
Published on 21-Sep-2021 08:00:40
Advertisements