• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get pandas Series Last Element in Python

Get pandas Series Last Element in Python

July 25, 2022 Leave a Comment

To get the last element of a pandas series, the easiest way is to use iloc and access the ‘-1’ indexed element.

import pandas as pd

s = pd.Series([0,1,2,3])

print(s.iloc[-1])

#Output:
3

You can also use iat to get the last element of a pandas series.

import pandas as pd

s = pd.Series([0,1,2,3])

print(s.iat[-1])

#Output:
3

The last way to get the last element of a pandas series is with tail() and item().

import pandas as pd

s = pd.Series([0,1,2,3])

print(s.tail(1).item())

#Output:
3

When working with collections of data in Python, the ability to get particular elements easily is very important.

One such case is if you are using pandas and want to get the last element of a series.

There are a few different ways you can access the last element of a pandas series which we will cover in this article.

Using iloc() to Get Last Element of pandas Series in Python

The first and easiest way you can get the last element of a pandas series in Python is with iloc.

iloc allows us to access elements using purely integer-location based indexing for selection by position.

In other words, you can access elements by passing an integer representing the position of the element you want to access.

To get the last element, you can pass ‘-1’ to iloc.

Below shows you how to get the last element of a pandas series using iloc in Python.

import pandas as pd

s = pd.Series([0,1,2,3])

print(s.iloc[-1])

#Output:
3

Using iat() to Get Last Element of pandas Series in Python

Another way you can get the last element of a series when using pandas is with iat.

Similar to iloc, you can use iat to access a single value based on an integer location.

Below shows you how to get the last element of a pandas series using iat in Python.

import pandas as pd

s = pd.Series([0,1,2,3])

print(s.iat[-1])

#Output:
3

Using tail() to Get Last Element of pandas Series in Python

One last way you can get the last element of a pandas series object is with tail() and item().

The tail() function returns a specified number of elements from the end of a pandas series and then you can use item() to get the value of the last element.

Below is a simple example showing you how to get the last element of a pandas series with tail() and item().

import pandas as pd

s = pd.Series([0,1,2,3])

print(s.tail(1).item())

#Output:
3

Get First Element of pandas Series with iloc in Python

If you want to instead get the first element of a pandas series, you can use iloc and access the first element.

Below is an example showing you how to get first element of a pandas series in Python.

import pandas as pd

s = pd.Series([0,1,2,3])

print(s.iloc[0])

#Output:
0

Hopefully this article has been useful for you to be able to get the last element of a pandas series in Python.

Other Articles You'll Also Like:

  • 1.  Python Trig – Using Trigonometric Functions in Python for Trigonometry
  • 2.  Python Split List into N Sublists
  • 3.  How to Remove Vowels from a String in Python
  • 4.  Create Empty File with Python
  • 5.  Python atan – Find Arctangent and Inverse Tangent of Number
  • 6.  pandas set_value – Using at() Function to Set a Value in DataFrame
  • 7.  pandas max – Find Maximum Value of Series or DataFrame
  • 8.  Remove Leading Zeros from String with lstrip() in Python
  • 9.  Python issuperset() Function – Check if Set is Superset of Another Set
  • 10.  Find Last Occurrence in String of Character or Substring in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy