close

How to access the last element in a Pandas series?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to access the last element in a Pandas series in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to access the last element in a Pandas series?

  1. How to access the last element in a Pandas series?

    Or use DataFrame.iloc or DataFrame.iat – but is necessary position of column by Index.get_loc:

  2. access the last element in a Pandas series

    Or use DataFrame.iloc or DataFrame.iat – but is necessary position of column by Index.get_loc:

Method 1

or select last value need Series.iloc or Series.iat, because df['col1'] return Series:

print (df['col1'].iloc[-1])
3
print (df['col1'].iat[-1])
3

Or convert Series to numpy array and select last:

print (df['col1'].values[-1])
3

Or use DataFrame.iloc or DataFrame.iat – but is necessary position of column by Index.get_loc:

print (df.iloc[-1, df.columns.get_loc('col1')])
3
print (df.iat[-1, df.columns.get_loc('col1')])
3

Or is possible use last value of index (necessary not duplicated) and select by DataFrame.loc:

print (df.loc[df.index[-1], 'col1'])
3

Method 2

You can also use tail:

print(df['col1'].tail(1).item())

Output:

3

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read