• 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 First Element in Python

Get pandas Series First Element in Python

July 25, 2022 Leave a Comment

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

import pandas as pd

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

print(s.iloc[0])

#Output:
0

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

import pandas as pd

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

print(s.iat[0])

#Output:
0

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

import pandas as pd

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

print(s.head(1).item())

#Output:
0

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 first element of a series.

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

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

The first and easiest way you can get the first 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 first element, you can pass ‘0’ to iloc.

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

import pandas as pd

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

print(s.iloc[0])

#Output:
0

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

Another way you can get the first 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 first element of a pandas series using iat in Python.

import pandas as pd

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

print(s.iat[0])

#Output:
0

Using head() to Get First Element of pandas Series in Python

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

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

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

import pandas as pd

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

print(s.head(1).item())

#Output:
0

Get Last Element of pandas Series with iloc in Python

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

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

import pandas as pd

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

print(s.iloc[-1])

#Output:
3

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

Other Articles You'll Also Like:

  • 1.  Get Day of Week from Datetime in pandas DataFrame
  • 2.  Drop Last Row of pandas DataFrame
  • 3.  Get Day of Year from Date in pandas DataFrame
  • 4.  Remove Extension from Filename in Python
  • 5.  How to Remove All Occurrences of a Character in a List Using Python
  • 6.  Are Dictionaries Mutable in Python? Yes, Dictionaries are Mutable
  • 7.  Difference Between // and / When Dividing Numbers in Python
  • 8.  Convert False to 0 in Python
  • 9.  Loop Through Files in Directory Using Python
  • 10.  Python Even or Odd – Check if Number is Even or Odd Using % Operator

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