In Pandas DataFrames, when you want to pinpoint and retrieve or update a single value (scalar value) by specifying a specific “row” and “column,” the at and iat accessors are provided as faster alternatives to loc and iloc.
- at: Specified by row label (index name) and column name.
- iat: Specified by row number and column number (integers).
This article explains how to retrieve and update values using these accessors.
Implementation Sample Code
Below is code that retrieves and modifies information for specific products using cafe menu data (price and calories).
import pandas as pd
def access_scalar_values():
"""
Function to manipulate single values in a DataFrame using at and iat
"""
# 1. Data Preparation
# DataFrame with drink names as index and price/calories as columns
menu_items = ["Espresso", "Latte", "Mocha", "Cappuccino", "Americano"]
menu_data = {
"Price": [350, 450, 500, 450, 380],
"Calories": [10, 150, 280, 120, 15]
}
df = pd.DataFrame(menu_data, index=menu_items)
print("--- Initial Menu Data ---")
print(df)
print("\n")
# 2. Retrieving value via at (Label Specification)
print("=== Retrieval via at (Label) ===")
# Get Price of Mocha
# Format: df.at[row_label, column_name]
mocha_price = df.at["Mocha", "Price"]
print(f"Price of Mocha: {mocha_price} Yen")
# 3. Retrieving value via iat (Position Specification)
print("\n=== Retrieval via iat (Position) ===")
# Get Calories of Cappuccino
# Cappuccino is 4th from top -> Row number 3
# Calories is 2nd from left -> Column number 1
# Format: df.iat[row_number, col_number]
cappuccino_cal = df.iat[3, 1]
print(f"Calories of Cappuccino: {cappuccino_cal} kcal")
# 4. Updating Data
print("\n=== Updating Values ===")
# Update Price of Americano
print(f"Before Update Americano Price: {df.at['Americano', 'Price']}")
# Assign value using at (Price revision)
df.at["Americano", "Price"] = 400
print(f"After Update Americano Price: {df.at['Americano', 'Price']}")
print("\n--- DataFrame After Update ---")
print(df)
if __name__ == "__main__":
access_scalar_values()
Execution Result
--- Initial Menu Data ---
Price Calories
Espresso 350 10
Latte 450 150
Mocha 500 280
Cappuccino 450 120
Americano 380 15
=== Retrieval via at (Label) ===
Price of Mocha: 500 Yen
=== Retrieval via iat (Position) ===
Calories of Cappuccino: 120 kcal
=== Updating Values ===
Before Update Americano Price: 380
After Update Americano Price: 400
--- DataFrame After Update ---
Price Calories
Espresso 350 10
Latte 450 150
Mocha 500 280
Cappuccino 450 120
Americano 400 15
Explanation: Differences from loc/iloc
Usually, loc and iloc are used for data access, but these are multi-functional accessors capable of handling “range selection” and retrieving “multiple rows/columns.”
In contrast, at and iat are specialized for accessing “single values (scalar values).” Therefore, when reading or writing individual values frequently within a loop process, using at / iat can be expected to improve processing speed.
- df.at[row, col]: Use when you want to identify the location by label (name).
- df.iat[row_int, col_int]: Use when you want to identify the location by number (position).
