Zugriff nach Label und Position
Rufen Sie Elemente mit .loc (Label) und .iloc (Integer-Position) ab und verstehen Sie den Unterschied zwischen beiden.
Zugriff nach Label und Position ist eine kostenlose Pandas & NumPy Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Pandas & NumPy Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Pandas & NumPy Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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 stopWhen .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 1Selecting 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 100Label 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: int64Setting 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 30Bracket 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 preferredAccessing 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 50at 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 accessQuick 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.
Häufig gestellte Fragen
Ist die Lektion „Zugriff nach Label und Position“ kostenlos?
Ja — der vollständige Text von „Zugriff nach Label und Position“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Pandas & NumPy Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Pandas & NumPy Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Zugriff nach Label und Position“?
Rufen Sie Elemente mit .loc (Label) und .iloc (Integer-Position) ab und verstehen Sie den Unterschied zwischen beiden. Du übst Pandas & NumPy Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Pandas & NumPy Academy zu starten?
Keine Vorkenntnisse erforderlich. Pandas & NumPy Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Zugriff nach Label und Position“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Pandas & NumPy Academy-Lektion Code schreiben und ausführen?
Ja. Jede Pandas & NumPy Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Eine Series erstellen
- Zugriff nach Label und Position
- Vektorisierte Operationen auf Series
- Nützliche Series-Methoden