Articles

How to get a value from a row in pandas dataframe? | loc and iloc in pandas

How to get a value from a row in pandas dataframe? | loc and iloc in pandas


In this Pandas tutorial, we will discuss:

  • how to get a value from a row in pandas dataframe?
  • get particular row based on index pandas
  • get multiple rows by index pandas
  • get value from specific row and column pandas
  • get value from pandas dataframe based on condition or get rows based on condition pandas or select rows based on condition pandas
  • understand function loc and iloc pandas

There are two  methods to get the value/s from rows in the pandas DataFrame - loc and iloc in pandas. We will discuss each of them one by one.

 

Lets first understand, what is a DataFrame?

 

What is DataFrame?

DataFrame is a data structure that contains rows  and columns. It will store the data in rows and columns. From this we can say this is an two dimensional data structure.

In Pandas, we can create by using a function called DataFrame(). Since this data structure is available in pandas, we have to get it by importing pandas module.

 

In Python, import keyword is used to import any kind of module.

Syntax:

import module_name as alias

where,

1.      module_name is the name of the module which we want to import. Here it is pandas

2.      alias is the module nickname which we assign to the module_name

 

import pandas as pd

we are aliasing pandas as pd.

 

pandas.DataFrame() is the method used to create dataframe.

Syntax:

pandas.DataFrame(data,columns,index)

where,

1.      data is the input list/dictionary to assign data to the dataframe

2.      columns is used to assign the columns in the dataframe

3.      index is used to assign the index/row labels

 

Note: Here we are creating a dataframe from the dictionary. So no need to pass columns as a separate parameter. Since dictionary key will take column name as key and value is probably the values in the dataframe.


Create DataFrame

Let’s create a sample DataFrame with 3 rows and 4 columns with building data with indices.

import pandas as pd

# create a dictionary with building data
building_data={'block_no':['ba-001','ba-002','ba-003'],
               'name':['villas ohri daba','CSD3','fgdb1'],
               'cost':[456700,12000,450000],
               'sqaure-feet':[5000,1200,4564]}
                              
# pass this  building_data to the DataFrame  
dataframe_object=pd.DataFrame(building_data,index=['building-1','building-2','building-3'])     

# display the dataframe
print(dataframe_object)

Output:

           block_no              name    cost  sqaure-feet
building-1   ba-001  villas ohri daba  456700         5000
building-2   ba-002              CSD3   12000         1200
building-3   ba-003             fgdb1  450000         4564

This is the output that we will get from above code snippet.


Method 1 : Using loc function

loc stands for location. This function will return the row value based on index label/index name.

Syntax:

dataframe_object.loc

where,

  • dataframe_object is the input dataframe.

We can have different scenarios to use this function.

Lets see each scenario one by one.

 

Scenario 1: Get particular row based on index pandas

Here , we will get particular row from dataframe based on row index or index label in pandas.

 

Syntax: To get particular row based on index pandas

dataframe_object.loc[‘index_label’]

where,

1.      dataframe_object is the input dataframe

2.      index_label’ represents index name

 

Return:

It will return a row of Series.

 

Note – Series is an one dimensional array data structure from pandas module.

Example:

In this example, we will return or get particular row from dataframe based on the index labels.

import pandas as pd

# create a dictionary with building data
building_data={'block_no':['ba-001','ba-002','ba-003'],
               'name':['villas ohri daba','CSD3','fgdb1'],
               'cost':[456700,12000,450000],
               'sqaure-feet':[5000,1200,4564]}
                              
# pass this  building_data to the DataFrame  with indices
dataframe_object=pd.DataFrame(building_data,index=['building-1','building-2','building-3'])     

# get the row for the index label - 'building-1'
print(dataframe_object.loc['building-1'])

# get the row for the index label - 'building-2'
print(dataframe_object.loc['building-2'])

# get the row for the index label - 'building-3'
print(dataframe_object.loc['building-3'])

Output: We get particular row from dataframe based on each index label as shown below

block_no                 ba-001
name           villas ohri daba
cost                     456700
sqaure-feet                5000
Name: building-1, dtype: object
block_no       ba-002
name             CSD3
cost            12000
sqaure-feet      1200
Name: building-2, dtype: object
block_no       ba-003
name            fgdb1
cost           450000
sqaure-feet      4564
Name: building-3, dtype: object

Now, lets see the next scenario on how to get a value from a row in pandas dataframe?


Scenario 2: Get multiple rows by index pandas

Here, we will get multiple rows by index pandas or get multiple rows at a time based on row indices or index labels.

Syntax:

dataframe_object.loc[[‘index_label’,’ index_label’,……………….]]

where,

1.      dataframe_object is the input dataframe

2.      index_label’ represents index name

 

Return:

It will return a DataFrame.

 

Example:

In this example, we will get multiple rows by index pandas. In other words, it return all rows at a time based on the index labels.

import pandas as pd

# create a dictionary with building data
building_data={'block_no':['ba-001','ba-002','ba-003'],
               'name':['villas ohri daba','CSD3','fgdb1'],
               'cost':[456700,12000,450000],
               'sqaure-feet':[5000,1200,4564]}
                              
# pass this  building_data to the DataFrame  with indices
dataframe_object=pd.DataFrame(building_data,index=['building-1','building-2','building-3'])     

# get the rows for the index labels - 'building-1','building-2' and 'building-3'
print(dataframe_object.loc[['building-1','building-2','building-3']])

Output: Here we get multiple rows by index pandas

            block_no              name    cost  sqaure-feet
building-1   ba-001  villas ohri daba  456700         5000
building-2   ba-002              CSD3   12000         1200
building-3   ba-003             fgdb1  450000         4564

Lets proceed to third scenario.


Scenario 3: Get value from specific row and column pandas

Here , we will get value from specific row and column pandas.

Syntax:

dataframe_object.loc[‘index_label’,’column_name’]

where,

1.      dataframe_object is the input dataframe

2.      index_label’ represents row name

3.      column_name represents the column name

 

Return:

It will return a value.

 

Example:

In this example, we will return all values based on the index and column labels. Lets see how to get value from specific row and column pandas.

import pandas as pd

# create a dictionary with building data
building_data={'block_no':['ba-001','ba-002','ba-003'],
               'name':['villas ohri daba','CSD3','fgdb1'],
               'cost':[456700,12000,450000],
               'sqaure-feet':[5000,1200,4564]}
                              
# pass this  building_data to the DataFrame  with indices
dataframe_object=pd.DataFrame(building_data,index=['building-1','building-2','building-3'])     

# get the value from building-1 and cost column
print(dataframe_object.loc['building-1','cost'])

# get the value from building-1 and block_no column
print(dataframe_object.loc['building-1','block_no'])

# get the value from building-1 and name column
print(dataframe_object.loc['building-1','name'])

# get the value from building-1 and sqaure-feet column
print(dataframe_object.loc['building-1','sqaure-feet'])

Output:

456700
ba-001
villas ohri daba
5000

This completes discussion on get value from specific row and column pandas. Lets see the next one.


Scenario 4: Get rows based on condition pandas

Here , we will get rows based on condition pandas or get rows present based on the condition applied on the column

Syntax:

dataframe_object.loc[dataframe_object[’column_name’] operator value]

where,

1.      dataframe_object is the input dataframe

2.      column_name represents the column name

3.      operator represents the relational or logical operator

4.      value is compared with column value

 

Example:

In this example, we will apply different conditions on cost and name columns to get the rows. In other words, we will  get rows based on condition pandas.

import pandas as pd

# create a dictionary with building data

building_data={'block_no':['ba-001','ba-002','ba-003'],

               'name':['villas ohri daba','CSD3','fgdb1'],

               'cost':[456700,12000,450000],

               'sqaure-feet':[5000,1200,4564]}

                             

# pass this  building_data to the DataFrame  with indices

dataframe_object=pd.DataFrame(building_data,index=['building-1','building-2','building-3'])     


# get the data based on cost column
# where cost greater than 400000

print(dataframe_object.loc[dataframe_object['cost'] > 400000])


# get the data based on cost column
# where name is  CSD3

print(dataframe_object.loc[dataframe_object['name'] == 'CSD3'])

Output: We get below output for select rows based on condition pandas

            block_no              name    cost  sqaure-feet
building-1   ba-001  villas ohri daba  456700         5000
building-3   ba-003             fgdb1  450000         4564
           block_no  name   cost  sqaure-feet
building-2   ba-002  CSD3  12000         1200

Here we can also select rows based on multiple condition pandas as shown.

 

We have already covered loc function.

Since this tutorial is going to cover loc and iloc in pandas so lets now move ahead to iloc function now.


Method 2 : Using iloc function

iloc stands for index location.

 

This function will return the row value based on row index position.

Syntax:

dataframe_object.iloc

where, dataframe_object is the input dataframe.

We can have different scenarios to use this function.

 

Note – indexing starts with 0.

Scenario 1: Get particular row based on index pandas.

Here , we will get particular row based on row index position.

Syntax:

dataframe_object.loc[‘index_position’]

where,

1.      dataframe_object is the input dataframe

2.      index_position’ represents row index

 

Return:

It will return a row of Series.

 

Note – Series is an one dimensional array data structure from pandas module.

Example:

In this example, we will return the rows based on the index position.

import pandas as pd

# create a dictionary with building data
building_data={'block_no':['ba-001','ba-002','ba-003'],
               'name':['villas ohri daba','CSD3','fgdb1'],
               'cost':[456700,12000,450000],
               'sqaure-feet':[5000,1200,4564]}
                              
# pass this  building_data to the DataFrame 
dataframe_object=pd.DataFrame(building_data)     

# get the row for the index position - 0
print(dataframe_object.iloc[0])

# get the row for the index position - 1
print(dataframe_object.iloc[1])

# get the row for the index position - 2
print(dataframe_object.iloc[2])

Output: Here we get particular row based on index pandas using iloc

block_no                 ba-001
name           villas ohri daba
cost                     456700
sqaure-feet                5000
Name: 0, dtype: object
block_no       ba-002
name             CSD3
cost            12000
sqaure-feet      1200
Name: 1, dtype: object
block_no       ba-003
name            fgdb1
cost           450000
sqaure-feet      4564
Name: 2, dtype: object

We have see how to get particular row based on index pandas. So lets proceed to next scenario.


Scenario 2: Get multiple rows based on index position.

Here , we will get multiple rows by index pandas or get multiple rows at a time based on row index positions.

Syntax:

dataframe_object.loc[[‘index_position’,’ index_ position’,……………….]]

where,

1.      dataframe_object is the input dataframe

2.      index_ position represents row index

 

Return:

It will return a DataFrame.

 

Example:

In this example, we will return all rows at a time based on the index positions. Lets see how to get multiple rows by index pandas

import pandas as pd

# create a dictionary with building data
building_data={'block_no':['ba-001','ba-002','ba-003'],
               'name':['villas ohri daba','CSD3','fgdb1'],
               'cost':[456700,12000,450000],
               'sqaure-feet':[5000,1200,4564]}
                              
# pass this  building_data to the DataFrame  with indices
dataframe_object=pd.DataFrame(building_data,index=['building-1','building-2','building-3'])     

# get the rows for the index positions - 0, 1 and 2
print(dataframe_object.iloc[[0,1,2]])

Output:

           block_no              name    cost  sqaure-feet
building-1   ba-001  villas ohri daba  456700         5000
building-2   ba-002              CSD3   12000         1200
building-3   ba-003             fgdb1  450000         4564

Conclusion

In this tutorial, we discussed how to get the rows based on index labels using loc function and index positions using iloc function with different scenarios. We used loc and iloc pandas or loc and iloc in pandas in almost all examples to have clear understanding.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Feb 19,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!