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