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.
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())
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Pergunte-me perguntas sobre este assunto
Resumir este capítulo
Mostrar exemplos do mundo real
Awesome!
Completion rate improved to 2.56
Filling NA values
Deslize para mostrar o menu
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.
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())
Obrigado pelo seu feedback!