Pandas – DataFrame.iat[]

In this article, we will discuss how to use Dataframe.iat[], with few examples.

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

Syntax is as follows,

pandas.DataFrame.iat[row_number , column_number]

Arguments:

  • row_number: As indexing starts from 0, so to select the value from the nth row, the row number should be n-1.
  • column_number: As indexing starts from 0, so to select the value from the nth column, the column number should be n-1.

Returns:

Advertisements

It returns a single value at the given row and column number from the DataFrame. Whereas, if any column or row number is out of bound, it will raise IndexError.

Let’s see some examples,

Dataframe.iat[] – 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 number 2 and column number 3

# Get Cell Value from row index position 2 and column index position 3
value = df.iat[2, 3]

print (value)

Output:

India

As indexing starts from 0 in Pandas, therefore,

  • Row number 2 points to the third row of the Dataframe
  • Column number 3 points to the fourth row of the Dataframe

Let’s see an example where we will try to fetch the cell value by giving out of bound row number i.e.

# Get Cell Value from row number 11 and column number 3
value = df.iat[11, 3]

print (value)

Output:

IndexError: index 11 is out of bounds for axis 0 with size 6

As row number 11 doesn’t exist in the dataframe, so it is an out-of-bound value. Therefore it returned an IndexError.

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 from row index position 2 and column index position 3
value = df.iat[2, 3]

print (value)

# Get Cell Value from row number 11 and column number 3
value = df.iat[11, 3]

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

India

IndexError: index 11 is out of bounds for axis 0 with size 6

Summary:

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

Advertisements

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