w3resource

Pandas Series property: loc

Access a group of rows and columns in Pandas

The loc property is used to access a group of rows and columns by label(s) or a boolean array.

.loc[] is primarily label based, but may also be used with a boolean array.

Allowed inputs are:

  • A single label, e.g. 5 or 'a', (note that 5 is interpreted as a label of the index, and never as an integer position along the index).
  • A list or array of labels, e.g. ['a', 'b', 'c'].
  • A slice object with labels, e.g. 'a':'f'.
  • A boolean array of the same length as the axis being sliced, e.g. [True, False, True].
  • A callable function with one argument (the calling Series or DataFrame) and that returns valid output for indexing (one of the above)

Syntax:

Series.loc
Pandas Series loc property

Raises: KeyError
when any items are not found

Example - Getting values:

Python-Pandas Code:

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

Output:

                  max_speed  shield
cobra               2       3
viper               6       5
sidewinder          9       8
Pandas Series loc property

Example - Single label. Note this returns the row as a Series:

Python-Pandas Code:

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

Output:

max_speed    6
shield       5
Name: viper, dtype: int64

Example - List of labels. Note using [[]] returns a DataFrame:

Python-Pandas Code:

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

Output:

                  max_speed  shield
viper               6       5
sidewinder          9       8
Pandas Series loc property

Example - Single label for row and column:

Python-Pandas Code:

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

Output:

3
Pandas Series loc property

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

Python-Pandas Code:

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

Output:

cobra    2
viper    6
Name: max_speed, dtype: int64
Pandas Series loc property

Example - Boolean list with the same length as the row axis:

Python-Pandas Code:

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

Output:

                  max_speed  shield
sidewinder          9       8
Pandas Series loc property

Example - Conditional that returns a boolean Series:

Python-Pandas Code:

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

Output:

       max_speed	shield
sidewinder	9	8
Pandas Series loc property

Example - Conditional that returns a boolean Series with column labels specified:

Python-Pandas Code:

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

Output:

        max_speed
sidewinder	9
Pandas Series loc property

Example - Callable that returns a boolean Series:

Python-Pandas Code:

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

Output:

        max_speed	shield
sidewinder	9	   8
Pandas Series loc property

Example - Setting values:

Set value for all items matching the list of labels

Python-Pandas Code:

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

Output:

                 max_speed  shield
cobra               2       3
viper               6      50
sidewinder          9      50
Pandas Series loc property

Example - Set value for an entire row:

Python-Pandas Code:

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

Output:

                 max_speed  shield
cobra              10      10
viper               6      50
sidewinder          9      50
Pandas Series loc property

Example - Set value for an entire column:

Python-Pandas Code:

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

Output:

                 max_speed	shield
cobra	                  40	10
viper	                  40	50
sidewinder	          40	50
Pandas Series loc property

Example - Set value for rows matching callable condition:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2, 3], [6, 5], [9, 8]],
     index=['cobra', 'viper', 'sidewinder'],
     columns=['max_speed', 'shield'])
df.loc[['viper', 'sidewinder'], ['shield']] = 50
df.loc[df['shield'] > 25] = 0
df

Output:

                   max_speed	shield
cobra	                   40	10
viper	                   0	0
sidewinder	           0  	0
Pandas Series loc property

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

Another example using integers for the index

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2, 3], [6, 5], [9, 8]],
     index=[3, 4, 5], columns=['max_speed', 'shield'])
df

Output:

       max_speed  shield
3          2       3
4          6       5
5          9       8

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

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2, 3], [6, 5], [9, 8]],
     index=[3, 4, 5], columns=['max_speed', 'shield'])
df.loc[3:5]

Output:

       max_speed  shield
3          2       3
4          6       5
5          9       8
Pandas Series loc property

Example - Getting values with a MultiIndex:

A number of examples using a DataFrame with a MultiIndex

Python-Pandas Code:

import numpy as np
import pandas as pd
tuples = [
   ('cobra', 's1'), ('cobra', 's2'),
   ('sidewinder', 's1'), ('sidewinder', 's2'),
   ('viper', 's2'), ('viper', 's3')
]

index = pd.MultiIndex.from_tuples(tuples)

values = [[6, 2], [0, 4], [20, 30],
         [1, 4], [5, 1], [36, 56]]
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
df

Output:

                    max_speed  shield
cobra      s1          6       2
           s2          0       4
sidewinder s1         20      30
           s2          1       4
viper      s2          5       1
           s3         36      56

Example - Single label. Note this returns a DataFrame with a single index:

Python-Pandas Code:

import numpy as np
import pandas as pd
tuples = [
   ('cobra', 's1'), ('cobra', 's2'),
   ('sidewinder', 's1'), ('sidewinder', 's2'),
   ('viper', 's2'), ('viper', 's3')
]

index = pd.MultiIndex.from_tuples(tuples)

values = [[6, 2], [0, 4], [20, 30],
         [1, 4], [5, 1], [36, 56]]
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
df.loc['cobra']

Output:

        max_speed  shield
s1          6       2
s2          0       4
Pandas Series loc property

Example - Single index tuple. Note this returns a Series:

Python-Pandas Code:

import numpy as np
import pandas as pd
tuples = [
   ('cobra', 's1'), ('cobra', 's2'),
   ('sidewinder', 's1'), ('sidewinder', 's2'),
   ('viper', 's2'), ('viper', 's3')
]

index = pd.MultiIndex.from_tuples(tuples)

values = [[6, 2], [0, 4], [20, 30],
         [1, 4], [5, 1], [36, 56]]
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
df.loc[('cobra', 's2')]

Output:

max_speed    0
shield       4
Name: (cobra, s2), dtype: int64
Pandas Series loc property

Example - Single label for row and column. Similar to passing in a tuple, this returns a Series:

Python-Pandas Code:

import numpy as np
import pandas as pd
tuples = [
   ('cobra', 's1'), ('cobra', 's2'),
   ('sidewinder', 's1'), ('sidewinder', 's2'),
   ('viper', 's2'), ('viper', 's3')
]

index = pd.MultiIndex.from_tuples(tuples)

values = [[6, 2], [0, 4], [20, 30],
         [1, 4], [5, 1], [36, 56]]
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
df.loc['cobra', 's1']

Output:

max_speed    6
shield       2
Name: (cobra, s1), dtype: int64
Pandas Series loc property

Example - Single tuple. Note using [[]] returns a DataFrame:

Python-Pandas Code:

import numpy as np
import pandas as pd
tuples = [
   ('cobra', 's1'), ('cobra', 's2'),
   ('sidewinder', 's1'), ('sidewinder', 's2'),
   ('viper', 's2'), ('viper', 's3')
]

index = pd.MultiIndex.from_tuples(tuples)

values = [[6, 2], [0, 4], [20, 30],
         [1, 4], [5, 1], [36, 56]]
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
df.loc[[('cobra', 's2')]]

Output:

          max_speed	shield
cobra	s2	      0 	4
Pandas Series loc property

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

Python-Pandas Code:

import numpy as np
import pandas as pd
tuples = [
   ('cobra', 's1'), ('cobra', 's2'),
   ('sidewinder', 's1'), ('sidewinder', 's2'),
   ('viper', 's2'), ('viper', 's3')
]

index = pd.MultiIndex.from_tuples(tuples)

values = [[6, 2], [0, 4], [20, 30],
         [1, 4], [5, 1], [36, 56]]
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
df.loc[('cobra', 's1'), 'shield']

Output:

2
Pandas Series loc property

Example - Slice from index tuple to single label:

Python-Pandas Code:

import numpy as np
import pandas as pd
tuples = [
   ('cobra', 's1'), ('cobra', 's2'),
   ('sidewinder', 's1'), ('sidewinder', 's2'),
   ('viper', 's2'), ('viper', 's3')
]

index = pd.MultiIndex.from_tuples(tuples)

values = [[6, 2], [0, 4], [20, 30],
         [1, 4], [5, 1], [36, 56]]
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
df.loc[('cobra', 's1'):'viper']

Output:

                     max_speed  shield
cobra      s1          6       2
           s2          0       4
sidewinder s1         20      30
           s2          1       4
viper      s2          5       1
           s3         36      56
Pandas Series loc property

Example - Slice from index tuple to index tuple:

Python-Pandas Code:

import numpy as np
import pandas as pd
tuples = [
   ('cobra', 's1'), ('cobra', 's2'),
   ('sidewinder', 's1'), ('sidewinder', 's2'),
   ('viper', 's2'), ('viper', 's3')
]

index = pd.MultiIndex.from_tuples(tuples)

values = [[6, 2], [0, 4], [20, 30],
         [1, 4], [5, 1], [36, 56]]
df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
df.loc[('cobra', 's1'):('viper', 's2')]

Output:

                     max_speed  shield
cobra      s1          6       2
           s2          0       4
sidewinder s1         20      30
           s2          1       4
viper      s2          5       1
Pandas Series loc property

Previous: Access a single value for a row/column pair in Pandas
Next: Access a group of rows and columns in Pandas



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-loc.php