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

Course Content

Data Manipulation using pandas

Filling NA valuesFilling 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.

Everything was clear?

Section 2. Chapter 7
course content

Course Content

Data Manipulation using pandas

Filling NA valuesFilling 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.

Everything was clear?

Section 2. Chapter 7
some-alt