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:

Advertisements

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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

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

Scroll to Top