Imputare con media, mediana o moda
Riempimenti sensati in base al tipo di colonna.
Imputare con media, mediana o moda è una lezione Data Science Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Data Science Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Data Science Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Imputare con media, mediana o moda» è gratuita?
Sì — il testo completo di «Imputare con media, mediana o moda» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Data Science Academy, passa a CoddyKit PRO. Il corso Data Science Academy include 4 lezioni in totale.
Cosa imparerò in «Imputare con media, mediana o moda»?
Riempimenti sensati in base al tipo di colonna. Eserciti Data Science Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Data Science Academy?
Non è richiesta alcuna esperienza precedente. Data Science Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Imputare con media, mediana o moda»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Data Science Academy?
Sì. Ogni lezione Data Science Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Trovare i NaN nascosti nella tabella
- Eliminare o riempire: scegliere con criterio
- Imputare con media, mediana o moda
- Correggere i dtypes e le righe duplicate