Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Filling NA values | Preprocessing Data: Part II
Data Manipulation using pandas

book
Filling NA values

As you remember, removing all the rows with NA values is not always the best solution. Let's learn another method, how to fill NA values in a dataframe.

If you want to fill NAs with specific values, use the .fillna() method. There are many options on what parameters you should use. Let's consider obligatory and the most appropriate ones:

  • value - defines value that should be set instead of NAs. Can be a dictionary in format {'column_name': value_to_replace}.

  • method - defines the method that should be used to fill NA values (for instance, 'ffill': propagate last valid observation forward to next valid backfill / 'bfill': use next valid observation to fill gap.

  • limit - number of consequtive NA values that should be filled. By default, this parameter isn't specified. For instance, we can fill the NA values within the 'omphtotinch', 'hmwkswk' columns with respective means.

# Importing the library
import pandas as pd

# Reading the file
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data4.csv')
# NA values before deleting
print("Before filling:", df[['omphtotinch', 'hmwkswk']].isna().sum())
# Fill NA values with respective means
df.fillna(value = {'omphtotinch': df.morgh.mean(), 'hmwkswk': df.hmwkswk.mean()}, inplace = True)
# NA values after filling
print("After filling:", df[['omphtotinch', 'hmwkswk']].isna().sum())
1234567891011
# Importing the library import pandas as pd # Reading the file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data4.csv') # NA values before deleting print("Before filling:", df[['omphtotinch', 'hmwkswk']].isna().sum()) # Fill NA values with respective means df.fillna(value = {'omphtotinch': df.morgh.mean(), 'hmwkswk': df.hmwkswk.mean()}, inplace = True) # NA values after filling print("After filling:", df[['omphtotinch', 'hmwkswk']].isna().sum())
copy

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 7

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt