Pandas – Dataframe.at[]

In this article, we will discuss how to use Dataframe.at[] in Pandas, with some examples.

In Pandas, the DataFrame provides a property at[], to access the single values from a Dataframe by their row and column label name.

Syntax is as follows,

pandas.DataFrame.at[row_label , column_name]

Arguments:

  • row_label : The Row Index Label Name
  • column_name : The column name

Returns:

It returns a single value at the given row and column from the DataFrame. Whereas, if any column or row name does not exist, it raises the KeyError.

Let’s see some examples,

Dataframe.at[] – Examples

Let’s first create a DataFrame from a list of tuples i.e.

import pandas as pd

# List of Tuples
students = [('jack',  34, 'Sydeny',    'Australia'),
            ('Riti',  30, 'Delhi',     'India'),
            ('Vikas', 31, 'Mumbai',    'India'),
            ('Neelu', 32, 'Bangalore', 'India'),
            ('John',  16, 'New York',   'US'),
            ('Mike',  17, 'las vegas',  'US')]

# Create a DataFrame from list of tuples
df = pd.DataFrame( students,
                   columns=['Name', 'Age', 'City', 'Country'],
                   index=['a', 'b', 'c', 'd', 'e', 'f'])

print(df)

Output:

    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US

Now get the cell value at row name ‘c’ and column name ‘City’

# Get Cell value at row 'c' and Column 'City'
value = df.at['c', 'City']

print (value)

Output:

Mumbai

It returned the cell value using row and column names. But what if any of the row or column name does not exist?

Let’s see an example where we will try to fetch the cell value by giving a row name that does not exist,

# Get Cell value at row 'z' and Column 'City'
value = df.at['z', 'City']

print (value)

Output:

KeyError: 'z'

As row number ‘z’ doesn’t exist in the dataframe, it is a KeyError.

The complete example is as follows,

import pandas as pd

# List of Tuples
students = [('jack',  34, 'Sydeny',    'Australia'),
            ('Riti',  30, 'Delhi',     'India'),
            ('Vikas', 31, 'Mumbai',    'India'),
            ('Neelu', 32, 'Bangalore', 'India'),
            ('John',  16, 'New York',   'US'),
            ('Mike',  17, 'las vegas',  'US')]

# Create a DataFrame from list of tuples
df = pd.DataFrame( students,
                   columns=['Name', 'Age', 'City', 'Country'],
                   index=['a', 'b', 'c', 'd', 'e', 'f'])

print(df)

# Get Cell value at row 'c' and Column 'City'
value = df.at['c', 'City']

print (value)

# Get Cell value at row 'z' and Column 'City'
value = df.at['z', 'City']

print (value)

Output:

    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US

Mumbai

KeyError: 'z'

Summary:

We can use the DataFrame.at[] to access a single cell value of Pandas Dataframe by row and column name.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top