.fillna()
Published May 25, 2022
Contribute to Docs
The .fillna()
function returns a new DataFrame
object with NA
values replaced with a specified value. The original DataFrame
object, used to call the method, remains unchanged.
Syntax
df = dataframevalue.fillna(value)
dataframevalue
is the DataFrame with the source data and value
is the value used to fill holes. value
can be a scalar such as 0
, or it can be a DataFrame specifying replacement values for each column. Column labels not in value
won’t be filled.
.fillna()
has the following parameters:
Parameter Name | Data Type | Usage |
---|---|---|
value |
scalar, dict, Series, Dataframe | Value used to fill holes. A scalar or a dict/Series/DataFrame specifying replacement values for each column. |
method |
‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None |
‘backfill’/‘bfill’ fills holes with next valid observation. ‘pad’/‘ffill’ fills holes with last valid observation. |
axis |
0/1 or ‘index’/‘columns’ | Axis along which to fill missing values. |
inplace |
bool | If True , alters the existing DataFrame rather than returning a new one. Defaults to False . |
limit |
int | Maximum consecutive items to back/forward fill. Defaults to None . |
Example
In the following example, the .fillna()
method is used to fill in NA
values in a DataFrame first with a scalar, then with a dict:
import pandas as pdimport numpy as npd = {'col 1' : [1,2,3,np.nan], 'col 2' : ['A','B',np.nan,'D'], 'col 3' : [5,np.nan,7,8], 'col 4' : [np.nan,'F','G','H']}df = pd.DataFrame(data = d)print(f'Original df:\n{df}\n')first_fillna = df.fillna(0)print(f'After first fillna():\n{first_fillna}\n')second_fillna = df.fillna({'col 1':0,'col 2':'X','col 3':0,'col 4':'X'})print(f'After second fillna():\n{second_fillna}\n')
The output from these instances of the .fillna()
method is shown below:
Original df:col 1 col 2 col 3 col 40 1.0 A 5.0 NaN1 2.0 B NaN F2 3.0 NaN 7.0 G3 NaN D 8.0 HAfter first fillna():col 1 col 2 col 3 col 40 1.0 A 5.0 01 2.0 B 0.0 F2 3.0 0 7.0 G3 0.0 D 8.0 HAfter second fillna():col 1 col 2 col 3 col 40 1.0 A 5.0 X1 2.0 B 0.0 F2 3.0 X 7.0 G3 0.0 D 8.0 H
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pandas on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours