Fill NA/NaN values of DataFrame
To fill NA or NaN values of DataFrame with other value in Pandas, call fillna() method on this DataFrame and pass the value to be filled with as argument.
In this tutorial, we will learn the syntax of DataFrame.fillna() method and how to use this method to fill NA values with some value like 0 or other.
Syntax
The syntax of pandas DataFrame.fillna() method is
DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)
where
Parameter | Value | Description |
---|---|---|
value | scalar, dict, Series, or DataFrame | 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. |
method | {‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}. default value is None. | 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. |
axis | {0 or ‘index’, 1 or ‘columns’}. default value is 0. | Axis along which to fill missing values. |
inplace | bool. default value is False. | 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). |
limit | int. default value is None. | 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. |
downcast | dict. default value is None. | 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). |
Return Value
- DataFrame or
- None if inplace=True.
Examples
Fill NA values with 0 in DataFrame
In the following program, we take a DataFrame containing NA (np.nan) values, and fill these NA (missing) values with 0.
Example.py
import pandas as pd
import numpy as np
data = {'col_0': [11, 22, 33, np.nan], 'col_1': [np.nan, 55, 77, 66]}
df = pd.DataFrame(data)
result = df.fillna(0)
print(result)
Output
col_0 col_1
0 11.0 0.0
1 22.0 55.0
2 33.0 77.0
3 0.0 66.0
Replace all NaN elements with 0s.
Fill NA values with Limit
We may limit the number of fillings along the axis using limit parameter.
In the following program, the DataFrame has three NaN values in the first column. If we pass limit=2, only the first two NaN values would be filled with the given value.
Example.py
import pandas as pd
import numpy as np
data = {'col_0': [11, np.nan, np.nan, np.nan], 'col_1': [np.nan, 55, 77, 66]}
df = pd.DataFrame(data)
result = df.fillna(0, limit=2)
print(result)
Output
col_0 col_1
0 11.0 0.0
1 0.0 55.0
2 0.0 77.0
3 NaN 66.0
Fill NA values only in Specific Column
To fill NA values only in a specific column, select those specific columns using DataFrame indexing and then call fillna() method.
In the following program, we fill NA values of col_0
only.
Example.py
import pandas as pd
import numpy as np
data = {'col_0': [11, np.nan, np.nan, np.nan], 'col_1': [np.nan, 55, 77, 66]}
df = pd.DataFrame(data)
df['col_0'].fillna(0, inplace=True)
print(df)
Output
col_0 col_1
0 11.0 NaN
1 0.0 55.0
2 0.0 77.0
3 0.0 66.0
Conclusion
In this Pandas Tutorial, we learned the syntax of DataFrame.fillna() method how to use this method to fill NA (missing) values with a specific value with examples.