Zugriff per Label oder Position
Wählen Sie loc oder iloc sicher aus.
Zugriff per Label oder Position ist eine kostenlose Data Science 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 Data Science Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Data Science Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Two Ways to Reach In
A Series has both labels and positions, so there are two ways to grab a value: by its label or by its position number. Knowing which to use keeps your code clear. 🎯
Our Sample Series
Here is a small Series with string labels. We will use it to compare both access styles throughout this lesson.
import pandas as pd
s = pd.Series([90, 75, 88], index=['Ada', 'Bo', 'Cy'])loc Uses Labels
The .loc accessor looks values up by their index label. You ask for a name and pandas returns that named value.
print(s.loc['Bo'])iloc Uses Positions
The .iloc accessor works by integer position instead, counting from zero like a normal list. Position 1 is the second value.
print(s.iloc[1])Same Value, Two Roads
With our Series, s.loc['Bo'] and s.iloc[1] return the same number. They reach it differently: one by name, one by spot.
print(s.loc['Bo'] == s.iloc[1])Slicing With iloc
Position slices behave like list slices: the end is excluded. So iloc[0:2] returns the first two values only.
print(s.iloc[0:2])Slicing With loc
Label slices are different: with .loc, the end label is included. Asking from 'Ada' to 'Bo' returns both endpoints.
print(s.loc['Ada':'Bo'])The Inclusive Surprise
That inclusive end is the most common mix-up. Remember: iloc drops the last position, while loc keeps the last label. 🧠
Grab Several at Once
Pass a list of labels to .loc to pull many values together. The result is a smaller Series in the order you asked.
print(s.loc[['Ada', 'Cy']])When Labels Are Numbers
If your index is integers, plain s[2] gets ambiguous. Using explicit .loc or .iloc removes all doubt about what you meant.
Pick With Confidence
Reach for .loc when you know the name, and .iloc when you know the spot. Being explicit makes your selections impossible to misread.
Quick Check
Test your grip on the two accessors.
Recap
You now choose .loc for labels and .iloc for positions, and you remember that loc slices include the end while iloc slices do not. Solid! 🎉
Häufig gestellte Fragen
Ist die Lektion „Zugriff per Label oder Position“ kostenlos?
Ja — der vollständige Text von „Zugriff per Label oder 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 Data Science Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Data Science Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Zugriff per Label oder Position“?
Wählen Sie loc oder iloc sicher aus. Du übst Data Science 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 Data Science Academy zu starten?
Keine Vorkenntnisse erforderlich. Data Science 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 per Label oder 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 Data Science Academy-Lektion Code schreiben und ausführen?
Ja. Jede Data Science 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 Spalte mit Name und Index
- Zugriff per Label oder Position
- Series-Mathematik und Alignment
- Werte zählen und erkennen