How to access a single value in pandas Series using the integer position?


The pandas.Series.iat attribute is used to access the single series element by using the position index value and It is very similar to the iloc in pandas instead of accessing a group of elements here we will access a single element.

The “iat” attribute takes an integer index value for getting and setting the element in a particular position. Let’s take some examples to access a single series element by using the “.iat” attribute.

Example 1

import pandas as pd

# create a series
s = pd.Series([65, 66, 67, 68, 69, 70])

print(s)

print('Output: ', s.iat[4])

Explanation

In this following example, we have created a series using a python list and the index is auto-created integer values by pandas.Series constructor. Here the “4” is an index position s.iat[4] used to get that positional element.

Output

0 65
1 66
2 67
3 68
4 69
5 70
dtype: int64

Output: 69

In the same way, we can get any value using positional index data. For the above example, the output “69” is located in 4 index position.

Example 2

import pandas as pd

# create a series
s = pd.Series([65, 66, 67, 68, 69, 70])

print(s)

s.iat[4] = 111

print('Output: ', s)

Explanation

Now let’s update the value “111” at the 4th index position of a given pandas series “iat” attribute.

Output

0 65
1 66
2 67
3 68
4 69
5 70
dtype: int64

Output:
0 65
1 66
2 67
3 68
4 111
5 70
dtype: int64

We successfully updated the value “111” at integer index position “4” by using the “.iat” property of the pandas.Series, we can observe both the series object in the above output block.

The “.iat” attribute will raise an “indexError” if the given integer index position is not found in the index range.

Updated on: 09-Mar-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements