Course Content
Data Manipulation using pandas
Data Manipulation using pandas
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())
Thanks for your feedback!