ラベルベースと位置ベースのアクセス
locとilocを自信を持って使い分けます
「ラベルベースと位置ベースのアクセス」はCoddyKit上の無料Data Science Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはData Science Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Data Science Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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! 🎉
よくある質問
「ラベルベースと位置ベースのアクセス」レッスンは無料ですか?
はい。「ラベルベースと位置ベースのアクセス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Data Science Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Data Science Academyコースには全4レッスンが含まれています。
「ラベルベースと位置ベースのアクセス」で何を学びますか?
locとilocを自信を持って使い分けます ブラウザで直接実行するハンズオンコードでData Science Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Data Science Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのData Science Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「ラベルベースと位置ベースのアクセス」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このData Science Academyレッスンでコードを書いて実行できますか?
はい。すべてのData Science Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 名前とインデックスを持つ列
- ラベルベースと位置ベースのアクセス
- Seriesの演算とアラインメント
- 値を数えて見つける