Pandas Tutorial Pandas References

Pandas DataFrame - fillna() function



The Pandas DataFrame fillna() function returns is used to fill NA/NaN values using the specified method. The syntax for using this function is mentioned below:

Syntax

DataFrame.fillna(value=None, method=None, axis=None, 
                 inplace=False, limit=None)

Parameters

value Optional. Specify value to use to fill holes (e.g. 0). Alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame) can be used. Values not in the dict/Series/DataFrame will not be filled. This value cannot be a list.
method Optional. Specify method to use for filling holes in reindexed Series. It can take values from {'backfill', 'bfill', 'pad', 'ffill', None}. Default is None. pad / ffill: use last valid observation to fill gap. backfill / bfill: use next valid observation to fill gap.
axis Optional. Specify axis along which to fill missing values. It can take values from {0 or 'index', 1 or 'columns'}
inplace Optional. If True, fill in-place. Default: False
limit Optional. If method is specified, this is the maximum number of consecutive NaN values to forward/backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. Must be greater than 0 if not None. Default is None.

Return Value

Returns DataFrame with missing values filled or None if inplace=True.

Example:

In the example below, a DataFrame df is created. The fillna() function is used to fill all NA/NaN values with 0.0.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, np.nan, 2, np.nan],
  "Salary": [60, np.nan, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#filling all NA/NaN values with 0.0
df_new = df.fillna(0)
print("\ndf.fillna(0) returns:")
print(df_new)

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John     5.0    60.0
Marry    NaN     NaN
Sam      2.0    65.0
Jo       NaN    59.0

df.fillna(0) returns:
       Bonus  Salary
John     5.0    60.0
Marry    0.0     0.0
Sam      2.0    65.0
Jo       0.0    59.0

Example: using method

Instead of using values, method can be used to fill NA/NaN values. Consider the example below:

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, np.nan, 2, np.nan],
  "Salary": [np.nan, np.nan, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#filling all NA/NaN values using bfill
df_new1 = df.fillna(method='bfill')
print("\ndf.fillna(method='bfill') returns:")
print(df_new1)

#filling all NA/NaN values using ffill
df_new2 = df.fillna(method='ffill')
print("\ndf.fillna(method='ffill') returns:")
print(df_new2)

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John     5.0     NaN
Marry    NaN     NaN
Sam      2.0    65.0
Jo       NaN    59.0

df.fillna(method='bfill') returns:
       Bonus  Salary
John     5.0    65.0
Marry    2.0    65.0
Sam      2.0    65.0
Jo       NaN    59.0

df.fillna(method='ffill') returns:
       Bonus  Salary
John     5.0     NaN
Marry    5.0     NaN
Sam      2.0    65.0
Jo       2.0    59.0

Example: fill values column wise

To fill NA/NaN values column-wise, a dictionary specifying which value to use for each column can be used. Consider the example below:

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, np.nan, 2, np.nan],
  "Salary": [np.nan, np.nan, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

values = {'Bonus': 3, 'Salary': 50}

print("The DataFrame is:")
print(df)

#filling all NA/NaN values using dictionary
df_new = df.fillna(value=values)
print("\ndf.fillna(value=values) returns:")
print(df_new)

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John     5.0     NaN
Marry    NaN     NaN
Sam      2.0    65.0
Jo       NaN    59.0

df.fillna(value=values) returns:
       Bonus  Salary
John     5.0    50.0
Marry    3.0    50.0
Sam      2.0    65.0
Jo       3.0    59.0

❮ Pandas DataFrame - Functions