Pandas Series: fillna() function
Fill NA/NaN values using the specified method
The fillna() function is used to fill NA/NaN values using the specified method.
Syntax:
Series.fillna(self, value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
value | 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). Values not in the dict/Series/DataFrame will not be filled. This value cannot be a list. | scalar, dict, Series, or DataFrame | Required |
method | Method to use for filling holes in reindexed Series pad / ffill: propagate last valid observation forward to next valid backfill / bfill: use next valid observation to fill gap. | {‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None} Default Value: None |
Required |
axis | Axis along which to fill missing values. | {0 or ‘index’} | Required |
inplace | If True, fill in-place. Note: this will modify any other views on this object (e.g., a no-copy slice for a column in a DataFrame). | bool Default Value: False |
Required |
limit | 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. | int Default Value: None |
Required |
downcast | A dict of item->dtype of what to downcast if possible, or the string ‘infer’ which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible). | dict Default Value: None |
Required |
Returns: Series- Object with missing values filled.
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame([[np.nan, 2, np.nan, 0],
[3, 4, np.nan, 1],
[5, np.nan, np.nan, 6],
[np.nan, 4, np.nan, 5]],
columns=list('PQRS'))
df
Output:
P Q R S 0 NaN 2.0 NaN 0 1 3.0 4.0 NaN 1 2 5.0 NaN NaN 6 3 NaN 4.0 NaN 5
Example - Replace all NaN elements with 0s:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame([[np.nan, 2, np.nan, 0],
[3, 4, np.nan, 1],
[5, np.nan, np.nan, 6],
[np.nan, 4, np.nan, 5]],
columns=list('PQRS'))
df.fillna(0)
Output:
P Q R S 0 0.0 2.0 0.0 0 1 3.0 4.0 0.0 1 2 5.0 0.0 0.0 6 3 0.0 4.0 0.0 5
Example - We can also propagate non-null values forward or backward:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame([[np.nan, 2, np.nan, 0],
[3, 4, np.nan, 1],
[5, np.nan, np.nan, 6],
[np.nan, 4, np.nan, 5]],
columns=list('PQRS'))
df.fillna(method='ffill')
Output:
P Q R S 0 NaN 2.0 NaN 0 1 3.0 4.0 NaN 1 2 5.0 4.0 NaN 6 3 5.0 4.0 NaN 5
Example - Replace all NaN elements in column ‘P’, ‘Q’, ‘R’, and ‘S’, with 0, 1, 2, and 3 respectively:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame([[np.nan, 2, np.nan, 0],
[3, 4, np.nan, 1],
[5, np.nan, np.nan, 6],
[np.nan, 4, np.nan, 5]],
columns=list('PQRS'))
values = {'P': 0, 'Q': 1, 'R': 2, 'S': 3}
df.fillna(value=values)
Output:
P Q R S 0 0.0 2.0 2.0 0 1 3.0 4.0 2.0 1 2 5.0 1.0 2.0 6 3 0.0 4.0 2.0 5
Example - Only replace the first NaN element:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame([[np.nan, 2, np.nan, 0],
[3, 4, np.nan, 1],
[5, np.nan, np.nan, 6],
[np.nan, 4, np.nan, 5]],
columns=list('PQRS'))
values = {'P': 0, 'Q': 1, 'R': 2, 'S': 3}
df.fillna(value=values, limit=1)
Output:
P Q R S 0 0.0 2.0 2.0 0 1 3.0 4.0 NaN 1 2 5.0 1.0 NaN 6 3 NaN 4.0 NaN 5
Previous: Analyze and drop Rows/Columns with Null values in a Pandas series
Next: Fill NA/missing values in a Pandas series
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/pandas/series/series-fillna.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics