What is the DataFrame.loc attribute in Pandas?

Overview

In Pandas, the loc attribute of a DataFrame is used to access a group of rows and columns of a DataFrame by using their labels.

Syntax

The loc attribute takes the syntax below:

DataFrame.loc
Syntax for the "loc" attribute in Pandas

Parameters

The loc attribute takes no parameter value. Rather, it takes the labels for the columns.

Return value

The loc attribute returns the value(s) of the specified row or column label.

Code example

Let's create the DataFrame in the example below:

import pandas as pd
# creating a list of objects
int_values = [1, 2, 2, 4, 5]
text_values = ['alpha', 'beta', 'beta', 'delta', 'epsilon']
float_values = [0.0, 0.25, 0.5, 0.75, 1.0]
# creating a dataframe from the list of objects
df = pd.DataFrame({"int_column": int_values, "text_column": text_values,
"float_column": float_values}, index = ["Row 1", "Row 2", "Row 3", "Row 4", "Row 5"])
# printing the dataframe
print("Data Frame:\n", df)
# accessing the first and second row of the dataframe
print("\nloc for 1st and 2nd row:")
print(df.loc[["Row 1", "Row 2"]])
# accessing the int_column has a value of 2 and where the text_column has a value, "beta"
print("\nFiltering DataFrame using loc:")
print(df.loc[(df["int_column"] == 2) & (df["text_column"]=="beta")])

Code explanation

  • Line 1: We import the pandas module.

  • Lines 4 and 6: We create a list of objects, text_values, int_values, and float_values.

  • Line 9: We create a DataFrame using the list of objects we created with the pandas.DataFrame() function. The name of the DataFrame is df.

  • Line 13: We print the DataFrame, df.

  • Line 17: We return the first and second row of the DataFrame using the loc attribute and pass their labels as an index to the attribute.

  • Line 21: We access the columns int_column where it has a value of 2 and the other column text_column has a value, “beta”.

Free Resources