Pandas DataFrame.loc[] Function

Minahil Noor Jan 30, 2023
  1. Syntax of pandas.DataFrame.loc[]:
  2. Example Codes: DataFrame.loc[] Method to Locate a Specific Row
  3. Example Codes: DataFrame.loc[] Method to Locate Multiple Rows
  4. Example Codes: DataFrame.loc[] Method to Locate the Data of a Specific Row and Column
Pandas DataFrame.loc[] Function

Python Pandas DataFrame.loc[] function locates the values of the DataFrame according to the passed inputs.

Syntax of pandas.DataFrame.loc[]:

DataFrame.loc[]

Parameters

This function accepts no parameters. However, it has several input methods. You can read it here.

Return

It returns the required value or values.

Example Codes: DataFrame.loc[] Method to Locate a Specific Row

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})

print("The Original Data frame is: \n")
print(dataframe)

dataframe1 = dataframe.loc[0]
print("The Row Data is: \n")
print(dataframe1)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Row Data is: 

Attendance            60
Name              Olivia
Obtained Marks        90
Name: 0, dtype: object

The function has returned the row data of row 0.

Example Codes: DataFrame.loc[] Method to Locate Multiple Rows

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})

print("The Original Data frame is: \n")
print(dataframe)

dataframe1 = dataframe.loc[[0, 1, 2]]
print("The Rows are: \n")
print(dataframe1)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Rows are: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82

Now it has returned the multiple rows of the DataFrame.

Example Codes: DataFrame.loc[] Method to Locate the Data of a Specific Row and Column

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})

print("The Original Data frame is: \n")
print(dataframe)

dataframe1 = dataframe.loc[0, 'Attendance']
print("The Value is: \n")
print(dataframe1)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Value is: 

60

The function has returned the value of Attendance at the index 0.

Related Article - Pandas DataFrame