Pandas Fill NA – DataFrame.fillna()
Fill NA values in pandas DataFrame
Your data may have NAs (Not Available) values within your DataFrame. Think of these as blank, null, or not present values. Many pandas functions will give you a hard time if you use them with NAs. That is where Pandas Fill NA .fillna()
comes into play.
Pandas Fill NA will fill in your DataFrame <NA> values with another value of your choice. You can also “backfill” or “forwardfill” your cells with other values from the DataFrame.
Pseudo code: With all of my NA values, fill them in with something concrete.
Pandas Fill NA
Fill NA Parameters
.fillna()
starts off simple, but unlocks a ton of value once you start backfilling and forward filling. Let’s take a look at the parameters
- value (scalar, dict, Series, or DataFrame: This single parameter has a ton of value packed into it. Let’s take a look at each option. Take a look at the example below for reference to these.
- Scalar: Fill in your DataFrame’s missing values with a single other value.
- Dict: Fill in your missing values with different values depending on the index. Ex: Change your “fill values” depending on the column or row.
- Series: Same as dict above, you can customize your fill values based on the index. Make sure that your Series index references your DataFrame-to-be-filled’s index.
- DataFrame: The big one, fill in another DataFrame with values from another DataFrame.
- Method (‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None), default None: *Awesome parameter alert* This parameter will fill your NAs with the last known observation or next known observation. It is great when you want to fill your NAs with other values within your DataFrame.
- Backfill/bfill – Fill in your NAs with the next known observation
- ffill – Or also known as “forward fill”. This will fill your NAs with the last known observation.
- Axis (‘index’ or ‘columns’): You can use the method above to backfill or forward fill along an axis. Do you want your row values to propagate to the NAs? Or your column values?
- Inplace: If true, this will fill in your DataFrame inplace, meaning a copy will not be returned and your old DataFrame will be overwritten.
- Limit: The number of NA values you wish to fill forward/backward. Any cell that is further than your limit from the last/first known observation will continue to be NA. Check out the examples below to see this in action.
Pandas Fill NA has a ton of flexibility. The range of this function is best learned through examples. Let’s fun through a couple: