Tutorials References Menu

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