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
input_data
 is represents a list of datacolumns
 represent the columns names for the dataindex
 represent 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 inside loc[]
 function.
Syntax:
dataframe.loc[:,['column',........,'column']]
where,
- dataframe is the input dataframe
- column refers to the column names
:
 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,
- dataframe is the input dataframe
start_column_index
 refers to the starting columnend_column_index
 refers to the ending column:
 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,
- dataframe is the input dataframe
index_label
is the index label or index positioncolumn_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,
- dataframe is the input dataframe
row_index
is the index position of a rowcolumn_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[]
loc[]
andiloc[]
are nearly similar -loc[]
will return the entire row based on row label butiloc[]
will also return the entire row based on row index.at[]
andiat[]
are nearly similar -at[]
will return the data from dataframe based on row position/index and column name butiat[]
will also return the the data from dataframe based on row index/position and column index/position.loc[]
is label based andiloc[]
is position basedat[]
andiat[]
are used to access only single element from a dataframe butloc[]
andiloc[]
are used to access one or more elementsat[]
andiat[]
computation is faster thanloc[]
andiloc[]
- We can use
loc[]
andiloc[]
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