平均、中央値、最頻値で補完する
列の型に応じた適切な補完
「平均、中央値、最頻値で補完する」はCoddyKit上の無料Data Science Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはData Science Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Data Science Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Filling With a Statistic
Instead of inventing random numbers, you fill gaps with a summary value from the column itself. This careful approach is called imputation. 🧮
The Mean Fill
For numbers, the simplest choice is the mean, the column average. It keeps the overall total roughly intact when gaps are few.
df['age'].fillna(df['age'].mean())When the Mean Misleads
The mean is fragile: a few huge values drag it far from the center. On skewed data, mean imputation can pull every gap toward an unrealistic number.
The Median Fill
The median is the middle value, and it shrugs off extreme outliers. For skewed numeric columns it is usually the safer fill.
df['income'].fillna(df['income'].median())Mean or Median?
A quick rule: reach for the median when a column has outliers or a long tail, and the mean only when values are fairly symmetric.
The Mode for Categories
Text and category columns have no average. For them you fill with the mode, the most frequent value, since that is the most likely fit.
top = df['city'].mode()[0]
df['city'].fillna(top)Why mode Returns a Series
A column can tie for most common, so mode() returns a Series of all winners. Pick the first with index 0 when you need one value.
df['city'].mode()[0]Fill Per Column Type
The best practice is to impute each column by its type: median for skewed numbers, mean for symmetric ones, and mode for categories.
Forward and Back Fill
For ordered data like time series, carry the last known value forward with ffill, or pull the next one backward with bfill.
df['temp'].ffill()The Hidden Cost
Every imputation shrinks the column's natural spread, because filled values cluster at one point. Note this, since it nudges your variance downward.
Flag What You Filled
A pro habit: add a boolean column marking which rows were imputed. That flag lets later analysis know which values were real and which were guessed.
df['age_filled'] = df['age'].isna()Quick Check
An income column is heavily skewed by a few millionaires. Which imputation fits best?
Recap: Smart Fills
You now impute with mean, median, or mode by column type, use ffill for ordered data, and flag filled rows so nothing gets hidden.
よくある質問
「平均、中央値、最頻値で補完する」レッスンは無料ですか?
はい。「平均、中央値、最頻値で補完する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Data Science Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Data Science Academyコースには全4レッスンが含まれています。
「平均、中央値、最頻値で補完する」で何を学びますか?
列の型に応じた適切な補完 ブラウザで直接実行するハンズオンコードでData Science Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Data Science Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのData Science Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「平均、中央値、最頻値で補完する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このData Science Academyレッスンでコードを書いて実行できますか?
はい。すべてのData Science Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- テーブルに潜むNaNを見つける
- 削除か補完か:賢く選ぶ
- 平均、中央値、最頻値で補完する
- dtypeと重複行を修正する