Pandas iat: Python is an excellent language for data analysis, due to a strong ecosystem of data-centric Python tools. One of these packages is Pandas, which makes importing and analyzing data a lot easier.
Pandas DataFrame iat[] Property:
Pandas .iat: The iat[] property of the pandas module can be used to get a single value for a row/column pair based on integer position.
In that both allow integer-based lookups, they are similar to iloc. If you simply need to get or set a single value in a DataFrame or Series, you should use iat[].
Note: If an integer position is out of bounds, an IndexError is thrown.
- Python Pandas DataFrame add() Function
- Python Pandas DataFrame plot.line() Function
- Python Pandas Series var() Function
Pandas DataFrame iat[] Property in Python
Example1
Here, the iat[] property is used to get and set the elements of the DataFrame.
Approach:
- Import pandas module using the import keyword.
- Import NumPy module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Print the given dataframe.
- Pass some random position(row, column) to the iat[] property to get the value at that given position
and print the result.
- Pass some random data to the random position(row, column) to the iat[] property to set
the value at that given position. - Print the dataframe after modification
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Import NumPy module using the import keyword.
import numpy as np
# Pass some random key-value pair(dictionary), index list as arguments to the
# DataFrame() function of the pandas module to create a dataframe
data_frme = pd.DataFrame({
"emp_name": ["john", "nick" , "jessy", "mary"],
"emp_age": [25, 35, 38, 22],
"emp_salary": [25000, 40000, 22000, 80000]},
index= [1, 2, 3, 4]
)
print("The given DataFrame:")
print(data_frme)
print()
# Pass some random position(row, column) to the iat[] property to get
# the value at that given position and print the result.
print("The value present at the given position[2, 1] in the dataframe:")
print(data_frme.iat[2, 1])
# Pass some random data to the random position(row, column) to the iat[] property to set
# the value at that given position
data_frme.iat[0, 1] = 60
print()
# Print the dataframe after modification
print("The dataframe after modification:")
print(data_frme)
Output:
The given DataFrame: emp_name emp_age emp_salary 1 john 25 25000 2 nick 35 40000 3 jessy 38 22000 4 mary 22 80000 The value present at the given position[2, 1] in the dataframe: 38 The dataframe after modification: emp_name emp_age emp_salary 1 john 60 25000 2 nick 35 40000 3 jessy 38 22000 4 mary 22 80000
Example2
The iat[] property can also be used to get the elements of a Series.
NOTE: The row, column always starts from 0 index.
Approach:
- Import pandas module using the import keyword.
- Import NumPy module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Print the given dataframe
- Pass the row number to the iloc[] and column number to the iat[] function
- to get the value at the given position and print the result
- Note: The row, col always starts from 0 index.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Import NumPy module using the import keyword.
import numpy as np
# Pass some random key-value pair(dictionary), index list as arguments to the
# DataFrame() function of the pandas module to create a dataframe
data_frme = pd.DataFrame({
"emp_name": ["john", "nick" , "jessy", "mary"],
"emp_age": [25, 35, 38, 22],
"emp_salary": [25000, 40000, 22000, 80000]},
index= [1, 2, 3, 4]
)
# Print the given dataFrame
print("The given DataFrame:")
print(data_frme)
print()
# Pass the row number to the iloc[] and column number to the iat[] function
# to get the value at the given position and print the result
# Note: The row, col always starts from 0 index.
print("The element present at 1st row, 2nd column:")
print(data_frme.iloc[1].iat[2])
Output:
The given DataFrame: emp_name emp_age emp_salary 1 john 25 25000 2 nick 35 40000 3 jessy 38 22000 4 mary 22 80000 The element present at 1st row, 2nd column: 40000