0Pricing
Pandas & NumPy Academy · Aula

Preenchendo valores ausentes

Substitua NaN por uma constante, pela média da coluna, por preenchimento para a frente ou para trás usando fillna() e seu parâmetro method.

Preenchendo valores ausentes é uma aula grátis de Pandas & NumPy 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 Pandas & NumPy Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Pandas & NumPy Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

Introduction to fillna()

Imputation means replacing missing values with a reasonable substitute rather than dropping the row. Pandas fillna() is the primary tool for this: it replaces every NaN in a Series or DataFrame with a value you specify. Unlike dropping, imputation preserves all rows, which is especially important when data is scarce or when the missing-data pattern is informative.

The simplest form passes a scalar: df['col'].fillna(0).

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'product': ['A', 'B', 'C', 'D'],
    'price': [10.0, np.nan, 30.0, np.nan]
})

# Fill all NaN in 'price' with 0
filled = df['price'].fillna(0)
print(filled)
# 0    10.0
# 1     0.0
# 2    30.0
# 3     0.0

Filling with a Column Mean or Median

Filling with the column mean (for symmetric distributions) or median (for skewed distributions) is one of the most common imputation strategies. These statistics represent the central tendency of the data, so they minimise the distortion of the column's distribution. Always compute the statistic on the training split to avoid data leakage.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'salary': [50000, np.nan, 70000, 80000, np.nan, 60000]
})

mean_salary = df['salary'].mean()
median_salary = df['salary'].median()

df['salary_mean_filled'] = df['salary'].fillna(mean_salary)
df['salary_median_filled'] = df['salary'].fillna(median_salary)
print(df)
#     salary  salary_mean_filled  salary_median_filled
# 0  50000.0             50000.0               50000.0
# 1      NaN             65000.0               65000.0
# 2  70000.0             70000.0               70000.0

Filling Categoricals with Mode

For categorical columns, filling with the mean or median doesn't make sense. Instead, use the mode — the most frequently occurring value. Series.mode()[0] returns the most common value (the [0] handles the case where there are multiple modes).

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'status': ['active', 'inactive', None, 'active', None, 'active']
})

most_common = df['status'].mode()[0]
print('Mode:', most_common)  # active

df['status'] = df['status'].fillna(most_common)
print(df['status'].tolist())
# ['active', 'inactive', 'active', 'active', 'active', 'active']

Forward Fill with ffill()

Forward fill (also called last-observation-carried-forward, LOCF) propagates the last valid (non-NaN) value forward to fill subsequent NaN positions. It is ideal for time series data where a measurement stays constant until updated — for example, device status, price levels, or sensor readings that only change at specific events.

import pandas as pd
import numpy as np

price = pd.Series(
    [100, np.nan, np.nan, 105, np.nan, 110],
    index=pd.date_range('2024-01', periods=6, freq='ME')
)

filled = price.ffill()
print(filled)
# 2024-01-31    100.0
# 2024-02-29    100.0
# 2024-03-31    100.0
# 2024-04-30    105.0
# 2024-05-31    105.0
# 2024-06-30    110.0

Backward Fill with bfill()

Backward fill (next-observation-carried-backward, NOCB) propagates the next valid value backward to fill preceding NaN positions. This is useful when you know future data fills in missing past values — for example, if you receive end-of-month data and need to back-fill days in that month before the report arrives.

import pandas as pd
import numpy as np

df = pd.DataFrame({'value': [np.nan, np.nan, 3, np.nan, 5]})

print(df['value'].bfill())
# 0    3.0
# 1    3.0
# 2    3.0
# 3    5.0
# 4    5.0
# dtype: float64

limit Parameter for Forward and Backward Fill

Both ffill() and bfill() accept a limit parameter that restricts how many consecutive NaN values are filled. This is important when you want to carry a value forward only for a short gap — long gaps should remain NaN to signal that the data is genuinely missing, not just delayed.

import pandas as pd
import numpy as np

s = pd.Series([1.0, np.nan, np.nan, np.nan, 5.0])

# Fill only the first NaN gap, leave the rest
print(s.ffill(limit=1))
# 0    1.0
# 1    1.0   <- filled
# 2    NaN   <- still NaN (limit reached)
# 3    NaN
# 4    5.0

Filling Different Columns Differently

In a real DataFrame, different columns may need different fill strategies. Pass a dictionary to fillna() where keys are column names and values are the fill values. This applies column-specific imputation in a single call, which is cleaner than chaining multiple individual fillna calls.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'age': [25, np.nan, 30],
    'income': [50000, np.nan, 70000],
    'country': ['USA', np.nan, 'UK']
})

fill_values = {
    'age': df['age'].median(),
    'income': df['income'].mean(),
    'country': 'Unknown'
}

filled = df.fillna(fill_values)
print(filled)
#     age   income  country
# 0  25.0  50000.0      USA
# 1  27.5  60000.0  Unknown
# 2  30.0  70000.0       UK

Group-Aware Imputation

A more sophisticated strategy is to fill NaN values with the group mean or median rather than the overall column mean. For example, fill a missing salary with the median salary for that job category. This preserves subgroup structure and produces more realistic imputations than a global statistic.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'dept': ['Eng', 'Eng', 'HR', 'HR', 'Eng'],
    'salary': [90000, np.nan, 50000, np.nan, 110000]
})

# Fill salary NaN with the median salary for each department
df['salary'] = df.groupby('dept')['salary'].transform(
    lambda x: x.fillna(x.median())
)
print(df)
#    dept   salary
# 0   Eng  90000.0
# 1   Eng  100000.0   <- group median (90k+110k)/2
# 2    HR  50000.0
# 3    HR  50000.0
# 4   Eng  110000.0

Filling with a Constant Sentinel Value

Sometimes the right imputation is a sentinel value that signals 'unknown' rather than a real measurement. For example, -1 for unknown age, 'N/A' for unknown category, or False for an unknown boolean flag. Sentinel values are valid when downstream code explicitly checks for them and treats them differently from real data.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'user_id': [1, 2, 3],
    'referral_source': ['email', np.nan, 'ad'],
    'trial_days': [14, np.nan, 7]
})

df['referral_source'] = df['referral_source'].fillna('unknown')
df['trial_days'] = df['trial_days'].fillna(-1).astype(int)
print(df)
#    user_id referral_source  trial_days
# 0        1           email          14
# 1        2         unknown          -1
# 2        3              ad           7

Chaining fillna() in a Pipeline

Like all Pandas methods, fillna() returns a new DataFrame and integrates cleanly into a method chain. Calling .fillna() after .dropna() applies a two-step strategy: drop rows missing critical columns, then fill remaining optional NaN values with sensible defaults — all in one readable pipeline.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'user_id': [1, None, 3, 4],
    'score': [88, 95, np.nan, 76],
    'label': ['A', 'B', None, 'D']
})

cleaned = (
    df
    .dropna(subset=['user_id'])
    .fillna({'score': df['score'].mean(), 'label': 'Unknown'})
)
print(cleaned)
#    user_id  score    label
# 0      1.0   88.0        A
# 2      3.0   86.3  Unknown
# 3      4.0   76.0        D

Verifying No NaN Remains

After imputation, always verify that no NaN values remain in the columns you targeted. Call df.isna().sum() or assert that the count is zero. An unexpected non-zero count means your fillna was missing a case — perhaps a column had a dtype that prevented the fill, or you forgot a column in your dict.

import pandas as pd
import numpy as np

df = pd.DataFrame({'a': [1.0, np.nan, 3.0], 'b': [np.nan, 2.0, 3.0]})
filled = df.fillna(0)

# Verify
assert filled.isna().sum().sum() == 0, 'Some NaN remain!'
print('All NaN filled successfully')
print(filled.isna().sum())
# a    0
# b    0
# dtype: int64

Quick Check

Test your understanding of filling missing values in Pandas.

Lesson Recap

In this lesson you learned: fillna(scalar) replaces all NaN with a constant, fillna(mean/median) imputes with column statistics, and ffill()/bfill() propagate adjacent values for time series. Pass a dictionary to fillna() for column-specific strategies, and use groupby().transform() for group-aware imputation. Next up we cover interpolation and when to choose advanced imputation techniques.

Perguntas Frequentes

A aula “Preenchendo valores ausentes” é grátis?

Sim — o texto completo de “Preenchendo valores ausentes” é 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 Pandas & NumPy Academy, atualize para CoddyKit PRO. O curso de Pandas & NumPy Academy inclui 4 aulas no total.

O que vou aprender em “Preenchendo valores ausentes”?

Substitua NaN por uma constante, pela média da coluna, por preenchimento para a frente ou para trás usando fillna() e seu parâmetro method. Você pratica Pandas & NumPy 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 Pandas & NumPy Academy?

Nenhuma experiência prévia é necessária. Pandas & NumPy 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 “Preenchendo valores ausentes”?

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 Pandas & NumPy Academy?

Sim. Cada aula de Pandas & NumPy 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

  1. Detectando valores ausentes
  2. Removendo valores ausentes
  3. Preenchendo valores ausentes
  4. Interpolação e imputação avançada
← Voltar para Pandas & NumPy Academy