Pandas at[]

The at[] property in Pandas is used to get a single value from a DataFrame based on the row index and column name.

Example

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Los Angeles', 'Chicago']}

df = pd.DataFrame(data)

# access the Name from the row with index label 1 selected_name = df.at[1, 'Name']
print(selected_name) # Output: Bob

at[] Syntax

The syntax of the at[] property in Pandas is:

df.at[row_index, column_label]

at[] Arguments

The at[] property takes following arguments:

  • row_index - the label of the row from which you want to get the value
  • column_label - the label of the column from which you want to get the value

at[] Return Value

The at[] property in Pandas returns the value located at a specified row and column of the DataFrame.


Example 1: Select a Single Value Using at[] Property

import pandas as pd

# create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 28],
        'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']}

df = pd.DataFrame(data, index=['A', 'B', 'C', 'D'])

# select a single value by row and column label selected_value = df.at['A', 'Name']
print(selected_value)

Output

Alice

In the example above, df.at['A', 'Name'] is used to select the value in the row with index label A and the column labeled Name which is Alice.


Example 2: Setting Values with at[]

import pandas as pd

# create a DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'Salary': [50000, 60000, 70000]
}, index=['a', 'b', 'c'])

# use at[] to update the salary for the row labeled 'c' to 75000 df.at['c', 'Salary'] = 75000
print(df)

Output

      Name   Age  Salary
a    Alice   25   50000
b      Bob   30   60000
c  Charlie   35   75000

Here, df.at['c', 'Salary'] = 75000 changes the value of the Salary column for the row with the index c.

This updates the salary of Charlie from 70000 to 75000 in the df DataFrame.


Example 3: Using at[] With Condition

import pandas as pd

# create a DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'Salary': [50000, 60000, 70000]
}, index=['a', 'b', 'c'])

# display the original DataFrame
print("Original DataFrame:")
print(df)
print()

# increase the salary by 10% for employees whose age is greater or equal to 30 for i in df.index: if df.at[i, 'Age'] >= 30: df.at[i, 'Salary'] *= 1.10
# display the modified DataFrame print("\nModified DataFrame:") print(df)

Output

Original DataFrame:
      Name   Age  Salary
a    Alice   25   50000
b      Bob   30   60000
c  Charlie   35   70000

Modified DataFrame:
      Name  Age  Salary
a    Alice   25   50000
b      Bob   30   66000
c  Charlie   35   77000

In this example, we first created the df DataFrame with columns: Name, Age, and Salary.

Then, we iterated through df's index and checked the Age column for each employee.

If the age is greater than or equal to 30, we use the at[] property to access and modify the Salary column for that specific employee by multiplying it by 1.10 (increasing it by 10%).