0Pricing
Data Science Academy · Pelajaran

Akses berdasarkan Label vs Posisi

Pilih loc atau iloc dengan percaya diri.

Akses berdasarkan Label vs Posisi adalah pelajaran Data Science Academy gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Data Science Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Data Science Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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! 🎉

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Akses berdasarkan Label vs Posisi” gratis?

Ya — teks lengkap “Akses berdasarkan Label vs Posisi” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Data Science Academy, upgrade ke CoddyKit PRO. Kursus Data Science Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Akses berdasarkan Label vs Posisi”?

Pilih loc atau iloc dengan percaya diri. Kamu berlatih Data Science Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Data Science Academy?

Tidak diperlukan pengalaman sebelumnya. Data Science Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.

Berapa lama pelajaran “Akses berdasarkan Label vs Posisi” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Data Science Academy ini?

Ya. Setiap pelajaran Data Science Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Kolom dengan Nama dan Indeks
  2. Akses berdasarkan Label vs Posisi
  3. Matematika Series dan Penyelarasan
  4. Menghitung dan Menemukan Nilai
← Kembali ke Data Science Academy