.loc
StevenSwiniarski474 total contributions
Published May 12, 2022Updated May 23, 2022
Contribute to Docs
The .loc
property of the DataFrame
object allows the return of specified rows and/or columns from that DataFrame
. It can do so using a label or label(s), or a boolean array of the same size as the axis being filtered.
Syntax
dataframevalue.loc[rows,columns]
Note: .loc
is not a method, it is a property indexed via square brackets.
Inside the square brackets are specified indices for rows
and, optionally, specified indices for columns
.
The indices to access can be specified in several ways:
- A single label. Note: an integer is treated as a label, not a row or column position.
- An array of labels.
- An array of booleans the same size as the index being accessed.
- Labels using the Python slice notation. Note: slices for
.loc
will include both endpoints.
Example
The example below creates a DataFrame and uses the .loc
property to return various combinations of rows and columns:
import pandas as pdd = {'col 1' : [1,2,3,4], 'col 2' : ['A','B','C','D'], 'col 3' : [5,6,7,8], 'col 4' : ['E','F','G','H']}df = pd.DataFrame(data = d)print(f'Original df:\n{df}\n')first_loc = df.loc[:,'col 3']print(f'First .loc:\n{first_loc}\n')second_loc = df.loc[:2]print(f'Second .loc:\n{second_loc}\n')third_loc = df.loc[[0,3],'col 2':]print(f'Third .loc:\n{third_loc}')
This will print the following:
Original df:col 1 col 2 col 3 col 40 1 A 5 E1 2 B 6 F2 3 C 7 G3 4 D 8 HFirst .loc:0 51 62 73 8Name: col 3, dtype: int64Second .loc:col 1 col 2 col 3 col 40 1 A 5 E1 2 B 6 F2 3 C 7 GThird .loc:col 2 col 3 col 40 A 5 E3 D 8 H
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.