How to access a single value in pandas Series using the .at attribute?


The pandas.Series.at attribute is used to access the single labeled element from a Series object and It is very similar to the loc in pandas. The “at” attribute takes label data to get or set the series value in that particular label position.

It will return a single value based on the index label, and it also uploads the data at that particular label position.

Example 1

import pandas as pd

# create a sample series
s = pd.Series({'A':12,'B':78,"C":56})

print(s)

print(s.at['A'])

Explanation

In this following example, we have created a series using a python dictionary and the index will be labeled by using the keys in the dictionary. Here the “A” is a label in s.at['A'] used to represent the positional index of a value.

Output

A    12
B    78
C    56
dtype: int64

Output:
12

We can see both initialized series object and output for at attribute in the above block.

Example 2

import pandas as pd

# create a sample series
s = pd.Series([2,4,6,8,10])

print(s)

s.at[0]

Explanation

We initialized a Series object with no index. If no index is provided, we can consider the index of our series object starting from 0 and increment in steps of 1 up to n-1.

Output

0    2
1    4
2    6
3    8
4    10
dtype: int64

The value present in the positional index “0” is displayed in the above block which is “2”.

Example 3

import pandas as pd

# create a sample series
s = pd.Series({'A':12,'B':78,"C":56})

print(s)

s.at['A'] = 100

print("Updated value at index 'A' ")
print(s)

Explanation

Now let’s update the value “100” in the series object using at attribute, which is located at label “A” i,e Index “A”.

Output

A    12
B    78
C    56
dtype: int64

Updated value at index 'A'
A    100
B    78
C    56
dtype: int64

We have successfully updated the value “100” at label position “A”, we can see both the series object in the above output block.

Updated on: 09-Mar-2022

729 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements