Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
How to access a single value in pandas DataFrame using the integer positions?
The pandas.DataFrame.iat attribute is used to access a single value of the DataFrame using the row/column integer positions 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 the integer index values of both rows and columns for getting or setting the element in a particular place.
The attribute will raise an “IndexError” if the given integer position is out of bounds.
Example 1
In this following example, we have created a DataFrame, accessing the 2nd-row 1st column element by using the iat attribute.
# importing pandas package
import pandas as pd
# create a Pandas DataFrame
df = pd.DataFrame({'A':[1, 'u', 60], 'B':[12.2, 17.4, 34.34], 'C':list('XYZ')})
print("DataFrame:")
print(df)
# get a value using integer position
result = df.iat[1, 0]
print("Output:")
print(result)
Output
The output is given below −
DataFrame: A B C 0 1 12.20 X 1 u 17.40 Y 2 60 34.34 Z Output: u
We have successfully accessed the 2nd-row 1st column element by specifying 1, 0 to the iat attribute.
Example 2
Now, let’s update the value “50” at the 2nd-row 1st column using the “iat” attribute.
# importing pandas package
import pandas as pd
# create a Pandas DataFrame
df = pd.DataFrame({'A':[1, 'u', 60], 'B':[12.2, 17.4, 34.34], 'C':list('XYZ')})
print("DataFrame:")
print(df)
# set a value
df.iat[1, 0] = 50
print("Updated DataFrame:")
print(df)
Output
The output is as follows −
DataFrame: A B C 0 1 12.20 X 1 u 17.40 Y 2 60 34.34 Z Updated DataFrame: A B C 0 1 12.20 X 1 50 17.40 Y 2 60 34.34 Z
We successfully updated the value “50” at integer index position “1, 0” by using the “.iat” property of the pandas.DataFrame; we can observe both the DataFrame objects in the above output block.
