Imputar com média, mediana ou moda
Preenchimentos adequados para cada tipo de coluna.
Imputar com média, mediana ou moda é uma aula grátis de Data Science Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Data Science Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Data Science Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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.
Perguntas Frequentes
A aula “Imputar com média, mediana ou moda” é grátis?
Sim — o texto completo de “Imputar com média, mediana ou moda” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Data Science Academy, atualize para CoddyKit PRO. O curso de Data Science Academy inclui 4 aulas no total.
O que vou aprender em “Imputar com média, mediana ou moda”?
Preenchimentos adequados para cada tipo de coluna. Você pratica Data Science Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Data Science Academy?
Nenhuma experiência prévia é necessária. Data Science Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Imputar com média, mediana ou moda”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Data Science Academy?
Sim. Cada aula de Data Science Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Encontrar os NaNs ocultos na sua tabela
- Remover ou preencher: escolhendo com cuidado
- Imputar com média, mediana ou moda
- Corrigir dtypes e linhas duplicadas