0Pricing
Pandas & NumPy Academy · Aula

Acesso por rótulo e por posição

Recupere elementos com .loc (rótulo) e .iloc (posição inteira) e entenda a diferença entre os dois.

Acesso por rótulo e por posição é uma aula grátis de Pandas & NumPy Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Pandas & NumPy Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Pandas & NumPy Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

Two Ways to Access Series Elements

Pandas provides two primary accessors for selecting elements from a Series: .loc for label-based access and .iloc for integer position-based access. Understanding which to use and when is critical — using the wrong one on an integer-indexed Series produces confusing results, and Pandas 2.0 deprecated many ambiguous direct bracket usages.

import pandas as pd

s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(s.loc['b'])    # 20  -- by label
print(s.iloc[1])     # 20  -- by position

.loc: Label-Based Access

.loc accesses elements by their index label. You can pass a single label (returns a scalar), a list of labels (returns a Series), or a slice of labels (inclusive on both ends, unlike Python slices). If the label does not exist, Pandas raises a KeyError. Use .loc when labels are meaningful (dates, names, IDs).

import pandas as pd

s = pd.Series([10, 20, 30, 40, 50],
              index=['a', 'b', 'c', 'd', 'e'])

print(s.loc['c'])          # 30
print(s.loc[['a', 'c']])   # a:10 c:30
print(s.loc['b':'d'])      # b:20 c:30 d:40 -- inclusive

.iloc: Position-Based Access

.iloc accesses elements by their integer position (0-based), completely ignoring the index labels. This behaves like Python list indexing: negative positions count from the end, and slices follow Python's exclusive-stop convention. Use .iloc when you need the 'n-th' element regardless of its label.

import pandas as pd

s = pd.Series([10, 20, 30, 40, 50],
              index=['a', 'b', 'c', 'd', 'e'])

print(s.iloc[2])          # 30  -- third element
print(s.iloc[-1])         # 50  -- last element
print(s.iloc[1:4])        # b:20 c:30 d:40 -- exclusive stop

When .loc and .iloc Differ

The difference matters most for integer-labelled Series. If a Series has index labels [5, 10, 15], then s.loc[5] returns the element with label 5 (the first element) while s.iloc[5] raises an IndexError (only 3 elements exist). Always choose based on whether you mean 'the element labelled X' or 'the X-th element'.

import pandas as pd

s = pd.Series([100, 200, 300], index=[5, 10, 15])
print(s.loc[10])   # 200  -- element with label 10
print(s.iloc[1])   # 200  -- second element
# s.loc[1] would raise KeyError -- no label 1

Selecting Multiple Labels with .loc

Pass a list to .loc to select multiple elements in any order, potentially with repetitions. The result is a new Series with the same labels as the list you provided. This is similar to fancy indexing in NumPy but aligned on index labels rather than positions. It is commonly used to reorder or subset columns in analytical pipelines.

import pandas as pd

s = pd.Series({'Jan': 100, 'Feb': 200, 'Mar': 300, 'Apr': 400})

# Select Q1 months in reverse order
print(s.loc[['Mar', 'Feb', 'Jan']])
# Mar    300
# Feb    200
# Jan    100

Label Slicing with .loc

Unlike Python slices, .loc slices are inclusive on both ends. s.loc['b':'d'] returns elements with labels b, c, and d. This can be surprising if you are used to Python's exclusive-stop convention. Make sure the labels you slice between actually exist in the index, or the slice will return unexpected results silently.

import pandas as pd

s = pd.Series(range(5), index=['a', 'b', 'c', 'd', 'e'])
print(s.loc['b':'d'])
# b    1
# c    2
# d    3
# dtype: int64
# Note: 'd' is INCLUDED (inclusive stop)

Boolean Indexing with .loc

You can pass a boolean Series to .loc to filter elements. The boolean Series must have the same index as the target Series. This is the Pandas equivalent of NumPy boolean masking and supports compound conditions with & and |. It is the standard way to filter rows in DataFrames too.

import pandas as pd

s = pd.Series([3, 7, 2, 9, 1], index=['a', 'b', 'c', 'd', 'e'])
print(s.loc[s > 4])
# b    7
# d    9
# dtype: int64

Setting Values with .loc and .iloc

Both .loc and .iloc can appear on the left side of an assignment to modify values in place. This is the correct pattern — using chained indexing like s['a'] = 99 may trigger a SettingWithCopyWarning in DataFrames. For Series, direct label access is generally safe, but .loc is explicit and recommended.

import pandas as pd

s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
s.loc['b'] = 99
print(s)
# a    10
# b    99
# c    30

Bracket Access on a Series

Direct bracket notation s['label'] works for label access and is equivalent to s.loc['label'] for string indices. For integer indices the behaviour is ambiguous and was changed in Pandas 2.0 — it now always means label access. For clarity and future-proof code, always use .loc or .iloc explicitly rather than relying on bracket disambiguation.

import pandas as pd

s = pd.Series([10, 20, 30], index=['x', 'y', 'z'])
print(s['y'])      # 20  -- label access via brackets
print(s.loc['y']) # 20  -- explicit and preferred

Accessing Elements from DatetimeIndex

When a Series has a DatetimeIndex, .loc accepts partial date strings for convenient range selection. s.loc['2023'] returns all elements from 2023, and s.loc['2023-01':'2023-06'] returns the first half of 2023. This is one of Pandas' most ergonomic features for time series work.

import pandas as pd

dates = pd.date_range('2023-01-01', periods=5, freq='ME')
values = pd.Series([10, 20, 30, 40, 50], index=dates)
print(values.loc['2023-03':'2023-05'])
# 2023-03-31    30
# 2023-04-30    40
# 2023-05-31    50

at and iat for Single Element Speed

.at[label] and .iat[position] are optimised scalar accessors — faster than .loc and .iloc when accessing a single element in a tight loop, because they skip the overhead of returning a Series object. They cannot select multiple elements. Use them when you need maximum performance for repeated single-element lookups.

import pandas as pd

s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(s.at['b'])    # 20  -- label scalar access
print(s.iat[2])     # 30  -- position scalar access

Quick Check

Test your understanding of .loc and .iloc from this lesson.

Lesson Recap

In this lesson you learned: .loc accesses elements by index label with inclusive slice stops, .iloc accesses elements by integer position with exclusive slice stops like Python lists, and .at and .iat are faster scalar accessors for single-element lookups in loops. Next up we explore vectorized operations on Series and how Pandas handles index alignment automatically.

Perguntas Frequentes

A aula “Acesso por rótulo e por posição” é grátis?

Sim — o texto completo de “Acesso por rótulo e por posição” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Pandas & NumPy Academy, atualize para CoddyKit PRO. O curso de Pandas & NumPy Academy inclui 4 aulas no total.

O que vou aprender em “Acesso por rótulo e por posição”?

Recupere elementos com .loc (rótulo) e .iloc (posição inteira) e entenda a diferença entre os dois. Você pratica Pandas & NumPy Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Pandas & NumPy Academy?

Nenhuma experiência prévia é necessária. Pandas & NumPy Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Acesso por rótulo e por posição”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Pandas & NumPy Academy?

Sim. Cada aula de Pandas & NumPy Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Criando uma Series
  2. Acesso por rótulo e por posição
  3. Operações vetorizadas em Series
  4. Métodos úteis de Series
← Voltar para Pandas & NumPy Academy