Compare loc[] vs iloc[] vs at[] vs iat[] with Examples

Compare loc[] vs iloc[] vs at[] vs iat[] with Examples

In this tutorial we will discuss about loc[], iloc[], at[] and iat[] functions and their differences.

These functions are used to select one/more columns from a dataframe. They are available in the pandas dataframe

Lets create sample dataframe


Create pandas DataFrame with example data

DataFrame is a data structure used to store the data in two dimensional format. It is similar to table that stores the data in rows and columns. Rows represents the records/ tuples and columns refers to the attributes.

We can create the DataFrame by using**pandas.DataFrame()**method.

Syntax:

pandas.DataFrame(input_data,columns,index)

Parameters:

It will take mainly three parameters

  1. input_datais represents a list of data
  2. columnsrepresent the columns names for the data
  3. indexrepresent the row numbers/values

We can also create a DataFrame using dictionary by skipping columns and indices.

Example:Python Program to create a dataframe for market data from a dictionary of food items by specifying the column names.

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# display the dataframe
print(dataframe)

Output:

            id            name    cost  quantity
item-1  foo-23  ground-nut oil  567.00         1
item-2  foo-13         almonds  562.56         2
item-3  foo-02           flour   67.00         3
item-4  foo-31         cereals   76.09         2

You can learn more at Pandas dataframe explained with simple examples


Understanding the usage of loc[]

loc[] stands for location is used to select the data .

We need to specify the column names to be selected insideloc[]function.

Syntax:

dataframe.loc[:,['column',........,'column']]

where,

  1. dataframe is the input dataframe
  2. column refers to the column names
  3. :operator is used to select all rows from the column

Example 1:In this example, we are going to access the name, cost and quantity columns

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# display name ,cost and quantity columns from the dataframe
print(dataframe.loc[:,['name','cost','quantity']])

Output:

                  name    cost  quantity
item-1  ground-nut oil  567.00         1
item-2         almonds  562.56         2
item-3           flour   67.00         3
item-4         cereals   76.09         2

**Example 2:**Python program to select name and cost columns

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# display name ,cost  columns from the dataframe
print(dataframe.loc[:,['name','cost']])

Output:

                  name    cost
item-1  ground-nut oil  567.00
item-2         almonds  562.56
item-3           flour   67.00
item-4         cereals   76.09

Understanding the usage of iloc[]

iloc[] stands for location is used to select the data with index .

We need to specify the column indices to be selected inside iloc[]function.

Syntax:

dataframe.loc[:,['start_column_index':'end_column_index']]

where,

  1. dataframe is the input dataframe
  2. start_column_indexrefers to the starting column
  3. end_column_indexrefers to the ending column
  4. :operator is used to select all rows from the column

**Example 1:**Python program to select name, cost and quantity columns.

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# display name ,cost and quantity columns from the dataframe
print(dataframe.iloc[:,1:4])

Output:

                  name    cost  quantity
item-1  ground-nut oil  567.00         1
item-2         almonds  562.56         2
item-3           flour   67.00         3
item-4         cereals   76.09         2

**Example 2:**Python program to select name and cost columns

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# display name ,cost  columns from the dataframe
print(dataframe.iloc[:,1:3])

Output:

                  name    cost
item-1  ground-nut oil  567.00
item-2         almonds  562.56
item-3           flour   67.00
item-4         cereals   76.09

Understanding the usage of at[]

at[] is used to return the data in particular cell from the dataframe.

Syntax:

at[index_label,column_name]

where,

  1. dataframe is the input dataframe
  2. index_label is the index label or index position
  3. column_name is the column name

Example 1: Python program to get data using at[] by selecting 1 st , 2 nd and 3 rd element from name column

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# display 1 st , 2 nd and 3 rd element from name column

print(dataframe.at['item-1','name'])
print(dataframe.at['item-2','name'])
print(dataframe.at['item-3','name'])

Output:

ground-nut oil
almonds
flour

**Example 2:**Python program to get data using at[] by selecting 1st , 2nd and 3rd element from id column

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# display 1 st , 2 nd and 3 rd element from id column

print(dataframe.at['item-1','id'])
print(dataframe.at['item-2','id'])
print(dataframe.at['item-3','id'])

Output:

foo-23
foo-13
foo-02

Understanding the usage of iat[]

iat[] is used to return the data in particular cell from the dataframe based on row and column index.

Syntax:

at[row_index,column_index]

where,

  1. dataframe is the input dataframe
  2. row_index is the index position of a row
  3. column_index is the index position of a column

Example 1: Python program to get data using iat[] by selecting 1 st , 2 nd and 3 rd element from name column

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# display 1 st , 2 nd and 3 rd element from name column

print(dataframe.iat[0,1])
print(dataframe.iat[1,1])
print(dataframe.iat[2,1])

Output:

ground-nut oil
almonds
flour

**Example 2:**Python program to get data using iat[] by selecting 1st , 2nd and 3rd element from id column

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# display 1 st , 2 nd and 3 rd element from id column

print(dataframe.iat[0,0])
print(dataframe.iat[1,0])
print(dataframe.iat[2,0])

Output:

foo-23
foo-13
foo-02

Comparison between loc[] vs iloc[] vs at[] vs iat[]

  1. loc[] and iloc[] are nearly similar - loc[] will return the entire row based on row label but iloc[] will also return the entire row based on row index.
  2. at[] and iat[] are nearly similar - at[] will return the data from dataframe based on row position/index and column name but iat[] will also return the the data from dataframe based on row index/position and column index/position.
  3. loc[] is label based and iloc[] is position based
  4. at[] and iat[] are used to access only single element from a dataframe but loc[] and iloc[] are used to access one or more elements
  5. at[] and iat[] computation is faster than loc[] and iloc[]
  6. We can use loc[] and iloc[] to select data from one or more columns in a dataframe

Summary

In this article we discussed about loc[], iloc[], at[] and iat[] functions with syntax and examples. We have seen the exact differences among them using multiple examples.


References

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.