EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Pandas DataFrame.loc[]

By Anandkumar MurugesanAnandkumar Murugesan

Home » Software Development » Software Development Tutorials » Pandas Tutorial » Pandas DataFrame.loc[]

Pandas DataFrame.loc[]

Introduction to Pandas DataFrame.loc[]

A dataframe being a data structure formulated by means of row , column format. There will be always the necessity to locate across some specific values, some specific rows, or even drilling up to locating the values at field level. In the panda’s library, these functionalities are achieved by means of the Pandas DataFrame.loc[] method. The locate method allows us to classifiably locate each and every row, column, and fields in the dataframe in a precise manner. It also provides the capability to set values to these located instances. In this topic, we are going to learn about Pandas DataFrame.loc[].

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

DataFrame.loc(locationvalue)

Parameters:

Parameter Description
locationvalue The location value is the value which is been attempted to locate, here the row_id or the combination of row_id with another row_id could be mentioned to locate specific data.

Ex : loc( row_id )

This returns the entire row which is mentioned.

 

Examples of Pandas DataFrame.loc[]

Here are the following examples mention below

Example #1

Code:

import pandas as pd
Core_Dataframe = pd.DataFrame({'Emp_No' : ['Emp1','Emp2','Emp3','Emp4'],
'Employee_Name' :  ['Arun', 'selva', 'rakesh', 'arjith'],
'Employee_dept' : ['CAD', 'CAD', 'DEV', 'CAD']})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
print(" OUTPUTS FROM CORE DATADRAME LOCATE")
print("")
print(Core_Dataframe.loc[0])
print("")
print(Core_Dataframe.loc[2:3])
print("")
print(Core_Dataframe.loc[3])

Output:

Pandas DataFrame.loc[] output 1

Explanation:  In this example the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console.

We can notice at this instance the dataframe holds details like employee number, employee name and employee department. Over this dataframe the Pandas DataFrame.loc[] method is applied in three ways, in the first and third technique an entire row is located by specifying the index involved, in the second technique couple of rows are sliced by mentioning those index values in the loc().

Example #2

Code:

import pandas as pd
Core_Dataframe = pd.DataFrame({'A' :  [ 1, 6, 11, 15, 21, 26],
'B' :  [2, 7, 12, 17, 22, 27],
'C' :  [3, 8, 13, 18, 23, 28],
'D' :  [4, 9, 14, 19, 24, 29],
'E' :  [5, 10, 15, 20, 25, 30]})
print("")
Core_Dataframe.rename(index= { 0 : 'Row1',
1 : 'Row2',
2 : 'Row3',
3 : 'Row4',
4 : 'Row5',
5 : 'Row6'} , inplace =True)
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
print(" CORE DATAFRAME LOCATE ")
print(Core_Dataframe.loc[Core_Dataframe['A'] > 10])

Output:

Pandas DataFrame.loc[] output 2

Explanation:  In this example the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console.  We can notice at this instance the dataframe holds a random set of numbers and alphabetic values of columns associated with it. Here the loc() method is used for locating all rows for which the first column named ‘A’ has a value greater than 10. So  These rows alone get printed on to the console. 

Example #3

Code:

import pandas as pd
Core_Dataframe = pd.DataFrame( {
'name': ['Alan Xavier', 'Annabella', 'Janawong', 'Yistien', 'Robin sheperd', 'Amala paul', 'Nori'],
'city': ['california', 'Toronto', 'ontario', 'Shanghai',
'Manchester', 'Cairo', 'Osaka'],
'age': [51, 38, 23, 64, 18, 31, 47],
'py-score': [82.0, 73.0, 81.0, 30.0, 48.0, 61.0, 84.0] })
Core_Dataframe.set_index('name',inplace=True)
print("THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
print("CORE DATAFRAME LOCATE AND ALTER VALUE")
for iter in Core_Dataframe.iterrows():
if (Core_Dataframe.loc[iter[0],'py-score' ]) > 65:
Core_Dataframe.loc[iter[0], 'py-score' ] = 'pass'
print(Core_Dataframe)

Output:

Pandas DataFrame.loc[] output 3

Explanation:  In this example the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console.  We can notice at this instance the dataframe holds random people information and the py_score value of those people.

The key columns used in this dataframe are name, age, city, and py-score value. Here every row in the dataframe is iterated through a iterrows function and when the pyscore of the dataframe is greater than 65 then the user is considered as ‘pass’ and the same is updated in the py-score column. the dataframe after the update is printed on to the console.

Example #4

Code:

import pandas as pd
Core_Dataframe = pd.DataFrame({'Column1' :  [ 'A', 'B', 'C', 'D', 'E', 'F'],
'Column2' :  [ 'G', 'H', 'I', 'J', 'K', 'L'],
'Column3' :  [ 'M', 'N', 'O', 'P', 'Q', 'R'],
'Column4' :  [ 'S', 'T', 'U', 'V', 'W', 'X'],
'Column5' :  [ 'Y', 'Z', None, None, None, None]})
print("")
Core_Dataframe.rename(index= { 0 : 'Row1',
1 : 'Row2',
2 : 'Row3',
3 : 'Row4',
4 : 'Row5',
5 : 'Row6'} , inplace =True)
print("THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
print("CORE DATAFRAME LOCATE ")
print(Core_Dataframe.loc[ 'Row6': 'column5' ])

Output:

output 4

Explanation:  In this example the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console.  We can notice in this example the dataframe is associated with the values of alphabets in the English dictionary.

Every column in the dictionary is tagged with suitable column names. Here the Row index and the column index is used to slice and locate one specific row from the dataframe mentioned. the identified row is printed on to the console. 

Example #5

Code:

import pandas as pd
Core_Dataframe = pd.DataFrame({'A' :  [ 1.23, 6.66, 11.55, 15.44, 21.44, 26.4 ],
'B' :  [ 2.345, 745.5, 12.4, 17.34, 22.35, 27.44 ],
'C' :  [ 3.67, 8, 13.4, 18, 23, 28.44 ],
'D' :  [ 4.6788, 923.3, 14.5, 19, 24, 29.44 ],
'E' :  [ 5.3, 10.344, 15.556, 20.6775, 25.4455, 30.3 ]})
Core_Dataframe.rename(index= { 0 : 'Row1',
1 : 'Row2',
2 : 'Row3',
3 : 'Row4',
4 : 'Row5',
5 : 'Row6'} , inplace =True)
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
print(" CORE DATAFRAME LOCATE ONLY ONE COLUMN ")
print(Core_Dataframe.loc[ : , 'D' ])
print("")
print(" CORE DATAFRAME LOCATE ONLY ONE FIELD")
print(Core_Dataframe.loc[ 'Row1' , 'D' ])

Output:

output 5

Explanation:  In this example the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console.  A typical float dataset is used in this instance. Here this example displays how to locate only one specific column and even one specific field from the pandas dataframe.

Conclusion

The loc() is the most widely used function in pandas dataframe and the listed examples mention some of the most effective ways to use this function.

Recommended Articles

This is a guide to Pandas DataFrame.loc[]. Here we discuss the syntax and parameters of Pandas DataFrame.loc[] along with examples for better understanding. You may also look at the following articles to learn more –

  1. Pandas.Dropna()
  2. What is Pandas?
  3. Python Pandas Join
  4. Python Pandas DataFrame

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
Pandas Tutorial
  • Basic
    • Python Pandas DataFrame
    • Pandas Series
    • Pandas DataFrame.astype()
    • Pandas DataFrame.append()
    • Pandas iterrows()
    • Pandas DataFrame.transpose()
    • Pandas DataFrame.where()
    • Pandas Aggregate()
    • Pandas DataFrame.mean()
    • Pandas DataFrame.rename()
    • Pandas DataFrame.describe()
    • Pandas DataFrame.plot()
    • Pandas DataFrame.loc[]
    • Pandas Dataframe.iloc[]
    • Pandas DataFrame.sort()
    • Pandas DataFrame.fillna()
    • Pandas DataFrame.sample()
    • Pandas DataFrame.query()
    • Pandas DataFrame.groupby()
    • Pandas DataFrame.count()
    • Pandas drop_duplicates()
    • Pandas hist()
    • Pandas boxplot
    • Pandas shift()
    • Pandas assign()
    • Pandas Dataframe.join()
    • Pandas cut()
    • Pandas melt()
    • Pandas sum()
    • Pandas std()
    • Pandas to_frame()
    • Pandas DataFrame.merge()
    • Pandas DataFrame.filter()
    • Pandas For Loop
    • Pandas Index
    • Pandas Set Index
    • Pandas DataFrame.reindex
    • Pandas Transform
    • Python Pandas Join
    • Pandas left join
    • Pandas format
    • Pandas resample
    • Pandas Read File
    • Pandas Find Duplicates
    • Pandas Interpolate
    • Pandas to dict
    • Pandas Series to NumPy Array
    • Pandas DataFrame to excel
    • Pandas pivot_table()
    • Pandas Filter Rows
    • Pandas 3D DataFrame
    • Pandas Time Series
    • Pandas Timestamp
    • Pandas rolling
    • Pandas Interview Questions
    • Pandas bar plot
    • Pandas Lambda
    • Pandas split()
    • Pandas value_counts()
    • Pandas merge on multiple columns
    • Pandas Statistics
    • Pandas find
    • Pandas Convert Column to Int
    • Pandas Column
    • Pandas autocorrelation_plot
    • Pandas Sort by Column
    • Pandas Order by
    • Pandas DataFrame.head()
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More