The fillna()
function in the Pandas library is used to fill NA/NaN/None values by the specified given value. Values NA/NaN/None are considered missing values. By using this function you can also replace the missing values with the same value or replace missing values with different value by index.
In this article, I will explain the pandas.Series.fillna()
function, its syntax, parameters, and usage of how to fill the NA/NaN/None missing values with a specified given value.
Key Points –
- The
fillna()
function is used to replace missing values (NaN) in a Series with a specified value or method. - You can fill NaN values with a constant value, making it useful for imputation or filling gaps in data.
- The method parameter allows you to use different techniques for filling missing values, such as forward fill (
method='ffill'
) or backward fill (method='bfill'
). - The
limit
parameter can be specified to restrict the number of consecutive NaN values that will be filled. fillna()
can be applied to specific rows or columns of a DataFrame when dealing with a multi-dimensional structure.- The
fillna()
function returns a new Series by default, leaving the original Series unchanged unless modified in place.
Quick Examples of Pandas Series fillna()
Following are quick examples of the Pandas Series fillna() method.
# Quick examples of pandas series fillna()
# Example 1: Use Series.fillna() function
ser2 = ser.fillna(value = {2 :'Hyperion', 4 :'Hadoop', 5 :'Pega', 8:'R Programming'})
# Example 2: Pandas fillna NaN
# Replace same value
ser2 = ser.fillna('Hadoop')
# Example 3: Pandas series.fillna() function
# Using method = 'ffill'
ser2 = ser.fillna(method='ffill')
Pandas Series.fillna() Introduction
Following is the syntax of creating Series.fillna() method.
# Syntax of Series.fillna() function
Series.fillna(value=None, *, method=None, axis=None, inplace=False, limit=None, downcast=None)
Parameter of fillna()
Following are the parameters of the fillna().
value
– Scalar, dict, Series, or DataFrame. This is the value to use to fill the NaN values.method
– This is an interpolation method. It can be set to'ffill'
(forward fill) or'bfill'
(backward fill). This parameter is particularly useful for time series data.axis
– 0 or ‘index’, used to specify axis along which to fill missing values. For Series, this parameter is unused and defaults to 0.inplace
– If set to True, it modifies the Series in place and returns None. Default is False.limit
– This is an optional parameter that specifies the maximum number of consecutive NaN values to fill. It works in conjunction with themethod
parameter.downcast
– It’s used to downcast the result to a smaller data type if possible to save memory.downcast
– Additional keyword arguments can be passed and will be passed to thefillna
method of underlying arrays.
Return Value of fillna()
It returns a series of objects with fill missing values.
Initialize Pandas Series
The Pandas Series is a data structure in the Pandas library, which is one-dimensional and labeled with indices. It’s capable of storing various data types like strings, integers, floats, and other Python objects. Each element within the Series can be accessed using its corresponding default index.
To run some examples of the Pandas Series.fillna()
function, let’s create a series using a list of values.
import pandas as pd
import numpy as np
# Initialize pandas series
ser = pd.Series(['Java','Spark','PySpark',np.nan, 'Pandas','NumPy',None],
index=[0,1,2,3,4,5,6])
print(ser)
Yields below output.
# Output:
0 Java
1 Spark
2 None
3 PySpark
4 NaN
5 NaN
6 Pandas
7 NumPy
8 None
dtype: object
Use Series.fillna() Function
You can use Series.fillna()
function to fill out NA/NaN/None missing values in the given series object by index.
# Use Series.fillna() Function
ser2 = ser.fillna(value = {2 :'Hyperion', 4 :'Hadoop', 5 :'Pega', 8:'R Programming'})
print(ser2)
In the above example, if you pass the dictionary value 'Hyperion'
to the function along with the index Series.fillna(value={2:'Hyperion'})
, if NaN or None is there for Index 2, it will be replaced with 'Hyperion'
. Use a dictionary to pass the values to be filled corresponding to the different index labels in the series object.
# Output:
0 Java
1 Spark
2 Hyperion
3 PySpark
4 Hadoop
5 Pega
6 Pandas
7 NumPy
8 R Programming
dtype: object
Fill Value for all NaN
You can also use Series.fillna()
function to fill the same value for all missing values in the given series object.
# Pandas fillna NaN
ser2 = ser.fillna('Hadoop')
print(ser2)
In the above example, if you pass the value “Hadoop
” to the function Series.fillna("Hadoop")
, it replaces Hadoop
for all missing values.
# Output:
0 Java
1 Spark
2 Hadoop
3 PySpark
4 Hadoop
5 Hadoop
6 Pandas
7 NumPy
8 Hadoop
dtype: object
Pandas series.fillna() Function Using method=’ffill’
Similarly, you can also fill out the missing values in the given series object from the above row value by using the forward fill method to fill out the missing values automatically.
# Pandas series.fillna() function
# Using method = 'ffill'
ser2 = ser.fillna(method='ffill')
print(ser2)
In the above example, in series before NaN or None if you have a value 'Spark'
, and when you use Series.fillna()
function, then NaN or None values will automatically replace or be filled out with the before value 'Spark'
in the given series object.
# Output:
0 Java
1 Spark
2 Spark
3 PySpark
4 PySpark
5 PySpark
6 Pandas
7 NumPy
8 NumPy
dtype: object
Complete Example For Series fillna() Function
import pandas as pd
import numpy as np
# Initialize pandas series
ser = pd.Series(['Java','Spark','PySpark',np.nan, 'Pandas','NumPy',None],
index=[0,1,2,3,4,5,6])
print(ser)
# Use Series.fillna() function
ser2 = ser.fillna(value = {2 :'Hyperion', 4 :'Hadoop', 5 :'Pega', 8:'R Programming'})
print(ser2)
# Pandas fillna NaN
# With series object value
ser2 = ser.fillna('Hadoop')
print(ser2)
# Pandas series.fillna() function
# Using method = 'ffill'
ser2 = ser.fillna(method='ffill')
print(ser2)
Pandas Series.fillna() Function
It replaces NaN values in a Series with a specified value or method, helping maintain data consistency.
You can use a scalar value (like 0 or ‘missing’), a dictionary, or use methods like pad/ffill
(forward fill) or bfill
(backward fill).
Use the limit
parameter to specify the maximum number of consecutive NaN values to fill.
Pandas ensures the Series maintains its dtype where possible, but if the replacement value doesn’t match, it may upcast the Series dtype.
By default, fillna()
creates a new Series. Use inplace=True
to update the original Series.
The syntax is similar, but for DataFrames, you can fill NaNs column-wise or row-wise by setting the axis
parameter.
Conclusion
In this article, you have learned the pandas series fillna()
function to fill NA/NaN/None values with the specified value. This function allows you to replace the same value for all NaN/NA/None values and replace separate value by index position etc.
Happy Learning !!
Related Articles
- Pandas Series.clip() Function
- Pandas Series iloc[] Function
- Pandas series.str.get() Function
- Pandas Series round() Function
- Pandas Series.dtype() Function
- Pandas Series.isin() Function
- Pandas.Series.combine()
- Pandas Series sample() Function
- Pandas Series map() Function
- Pandas Series.min() Function
- Pandas Convert Series to Json
- Pandas Series reset_index() Function
- Pandas Series Drop duplicates() Function
- Pandas Series.str.contains() With Examples
- How to Make a Histogram in Pandas Series?
- Pandas Series.value_counts() With Examples