3

Pandas – DataFrame.iat[]

 2 years ago
source link: https://thispointer.com/pandas-dataframe-iat/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Pandas – DataFrame.iat[] – thisPointer

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]
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:

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.

Looking for a Career in Data Science or Machine Learning with Python?

Get a Professional Certificate in data Science by IBM. Build data science skills, learn Python & SQL, analyze & visualize data, build machine learning models. No degree or prior experience required. Checkout the detailed review.

Explore a new career path with a 7 day free trial.

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

print (value)

Output:

India
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)
# 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
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)
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
    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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK