Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Pandas DataFrame loc Property

❮ DataFrame Reference


Example

Return the age of Mary:

import pandas as pd

data = [[50, True], [40, False], [30, False]]
label_rows = ["Sally", "Mary", "John"]
label_cols = ["age", "qualified"]

df = pd.DataFrame(data, label_rows, label_cols)
print(df.loc["Mary", "age"])
Try it Yourself »

Definition and Usage

The loc property gets, or sets, the value(s) of the specified labels.

Specify both row and column with a label.

To access more than one row, use double brackets and specify the labels, separated by commas:

df.loc[["Sally", "John"]]

Specify columns by including their labels in another list:

df.loc[["Sally", "John"], ["age", "qualified"]]

You can also specify a slice of the DataFrame with from and to labels, separated by a colon:

df.loc["Sally": "John"]

;0

Note: When slicing, both from and to are included in the result.


Syntax

dataframe.loc[row, column)

Parameters

Parameter Description
row Optional. A label, or labels, specifying the label of the row(s)

column Optional. A label, or labels, specifying the label of the column(s)


Return Value

Depends on the input:

Single labels for both row and column ["Sally", "age"] returns the content of that cell.

Single label for one row ["Sally"] returns a Pandas Series.

A list of labels [["Sally", "Mary"]] returns a Pandas DataFrame.


❮ DataFrame Reference

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.