条件で行を絞り込む
DataFrameに対するブールマスク
「条件で行を絞り込む」はCoddyKit上の無料Data Science Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはData Science Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Data Science Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
From Columns to Rows
Picking columns trims width; filtering rows trims length. Now you keep only the records that match a condition you care about. 🔍
A Condition Makes a Mask
Write a comparison on a column and pandas returns a boolean mask: one True or False per row, telling you which records pass.
mask = df["age"] > 30
print(mask.head())Apply the Mask
Put the mask inside brackets and pandas keeps only the True rows. The result is a smaller DataFrame with the same columns.
adults = df[df["age"] > 30]Equality Filters
Use double equals to match a value exactly. Remember that a single equals would try to assign, not compare.
df[df["city"] == "Paris"]Not Equal and Comparisons
You can filter with the full set of comparisons: not-equal, greater-equal, and less-equal all work on a column.
df[df["score"] >= 80]
df[df["city"] != "Paris"]Filter on Text
String columns support a str accessor. Use str.contains to keep rows whose text includes a pattern.
df[df["email"].str.contains("@gmail")]Filter With between
For a numeric range, between reads more clearly than two separate comparisons and includes both ends by default.
df[df["age"].between(18, 65)]Keep Selected Columns Too
Combine row filtering with loc to grab matching rows and chosen columns in one readable line.
df.loc[df["age"] > 30, ["name", "age"]]The Mask Is Reusable
A mask is just a Series, so you can store it and reuse it across different selections without rewriting the condition.
senior = df["age"] > 60
df[senior]
df.loc[senior, "name"]Count the Matches
Because True counts as one, calling sum on a mask tells you how many rows pass before you even filter.
(df["age"] > 30).sum()Filtering Does Not Change df
Filtering returns a new DataFrame and leaves the original untouched. Assign the result to a variable to keep it.
young = df[df["age"] < 30]
# df itself is unchangedQuick Check
Recall how a comparison on a column behaves.
Recap: Filtering Rows
A condition builds a boolean mask, brackets apply it, and loc adds column selection. You can now keep exactly the records that matter. ✅
よくある質問
「条件で行を絞り込む」レッスンは無料ですか?
はい。「条件で行を絞り込む」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Data Science Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Data Science Academyコースには全4レッスンが含まれています。
「条件で行を絞り込む」で何を学びますか?
DataFrameに対するブールマスク ブラウザで直接実行するハンズオンコードでData Science Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Data Science Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのData Science Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「条件で行を絞り込む」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このData Science Academyレッスンでコードを書いて実行できますか?
はい。すべてのData Science Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。