Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame([[2, 3], [5, 6], [8, 9]],
     index=['cobra', 'viper', 'sidewinder'],
     columns=['max_speed', 'shield'])
df
Out[2]:
max_speed shield
cobra 2 3
viper 5 6
sidewinder 8 9

Pandas: DataFrame - Loc.

Single label:

In [3]:
df.loc['viper']
Out[3]:
max_speed    5
shield       6
Name: viper, dtype: int64

Pandas: DataFrame - Single label.

List of labels:

In [4]:
df.loc[['viper', 'sidewinder']]
Out[4]:
max_speed shield
viper 5 6
sidewinder 8 9

Pandas: DataFrame - List of labels.

Single label for row and column:

In [5]:
df.loc['cobra', 'shield']
Out[5]:
3

Pandas: DataFrame - Single label for row and column.

Slice with labels for row and single label for column. As mentioned above, note that both
the start and stop of the slice are included.

In [6]:
df.loc['cobra':'viper', 'max_speed']
Out[6]:
cobra    2
viper    5
Name: max_speed, dtype: int64

Pandas: DataFrame - Slice with labels for row and single label for column.

Boolean list with the same length as the row axis:

In [7]:
df.loc[[False, False, True]]
Out[7]:
max_speed shield
sidewinder 8 9

Pandas: DataFrame - Boolean list with the same length as the row axis.

Conditional that returns a boolean Series

In [8]:
df.loc[df['shield'] > 6]
Out[8]:
max_speed shield
sidewinder 8 9

Pandas: DataFrame - Conditional that returns a boolean Series.

Conditional that returns a boolean Series with column labels specified:

In [9]:
df.loc[df['shield'] > 6, ['max_speed']]
Out[9]:
max_speed
sidewinder 8

Pandas: DataFrame - Conditional that returns a boolean Series with column labels specified.

Callable that returns a boolean Series:

In [10]:
df.loc[lambda df: df['shield'] == 9]
Out[10]:
max_speed shield
sidewinder 8 9

Pandas: DataFrame - Callable that returns a boolean Series.

Setting values
Set value for all items matching the list of labels:

In [11]:
df.loc[['viper', 'sidewinder'], ['shield']] = 30
df
Out[11]:
max_speed shield
cobra 2 3
viper 5 30
sidewinder 8 30

Set value for an entire row:

In [12]:
df.loc['cobra'] =10
df
Out[12]:
max_speed shield
cobra 10 10
viper 5 30
sidewinder 8 30

Set value for an entire column:

In [13]:
df.loc[:, 'max_speed'] = 30
df
Out[13]:
max_speed shield
cobra 30 10
viper 30 30
sidewinder 30 30

Set value for rows matching callable condition:

In [14]:
df.loc[df['shield'] > 35] = 0
df
Out[14]:
max_speed shield
cobra 30 10
viper 30 30
sidewinder 30 30

Getting values on a DataFrame with an index that has integer labels

In [15]:
df = pd.DataFrame([[2, 3], [5, 6], [8, 9]],
     index=[2, 3, 4], columns=['max_speed', 'shield'])
df
Out[15]:
max_speed shield
2 2 3
3 5 6
4 8 9

Slice with integer labels for rows. As mentioned above, note that both the start and stop
of the slice are included:

In [16]:
df.loc[2:4]
Out[16]:
max_speed shield
2 2 3
3 5 6
4 8 9

Getting values with a MultiIndex
A number of examples using a DataFrame with a MultiIndex:

In [17]:
tuples = [
   ('cobra', 'm1'), ('cobra', 'm2'),
   ('sidewinder', 'm1'), ('sidewinder', 'm2'),
   ('viper', 'm2'), ('viper', 'm3')
]
In [18]:
index = pd.MultiIndex.from_tuples(tuples)
values = [[14, 4], [0, 6], [20, 30],
         [2, 4], [5, 1], [26, 46]]
In [19]:
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
df
Out[19]:
max_speed shield
cobra m1 14 4
m2 0 6
sidewinder m1 20 30
m2 2 4
viper m2 5 1
m3 26 46

Single label:

In [20]:
df.loc['cobra']
Out[20]:
max_speed shield
m1 14 4
m2 0 6

Single index tuple:

In [21]:
df.loc[('cobra', 'm2')]
Out[21]:
max_speed    0
shield       6
Name: (cobra, m2), dtype: int64

Single label for row and column:

In [22]:
df.loc['cobra', 'm1']
Out[22]:
max_speed    14
shield        4
Name: (cobra, m1), dtype: int64

Single tuple:

In [23]:
df.loc[[('cobra', 'm2')]]
Out[23]:
max_speed shield
cobra m2 0 6

Single tuple for the index with a single label for the column:

In [24]:
df.loc[('cobra', 'm1'), 'shield']
Out[24]:
4

Slice from index tuple to single label:

In [25]:
df.loc[('cobra', 'm1'):'viper']
Out[25]:
max_speed shield
cobra m1 14 4
m2 0 6
sidewinder m1 20 30
m2 2 4
viper m2 5 1
m3 26 46

Slice from index tuple to index tuple:

In [26]:
df.loc[('cobra', 'm1'):('viper', 'm2')]
Out[26]:
max_speed shield
cobra m1 14 4
m2 0 6
sidewinder m1 20 30
m2 2 4
viper m2 5 1