• Post author:
  • Post category:Pandas
  • Post last modified:May 22, 2024
  • Reading time:10 mins read
You are currently viewing Pandas Series.fillna() Function

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.

Advertisements

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.

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 the method 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 the fillna 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. For 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 replace with 'Hyperion'. Use a dictionary to pass the values to be filled corresponding to the different index labels in the series object.


# Use Series.fillna() Function
ser2 = ser.fillna(value = {2 :'Hyperion', 4 :'Hadoop', 5 :'Pega', 8:'R Programming'})
print(ser2)

Yields below output.


# 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. For example, if you pass the value “Hadoop” to the function Series.fillna("Hadoop"), it replaces Hadoop for all missing values.


# Pandas fillna NaN 
ser2 = ser.fillna('Hadoop')  
print(ser2)

Yields below output.


# 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. For 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.


# Pandas series.fillna() function 
# Using method = 'ffill'
ser2 = ser.fillna(method='ffill')
print(ser2)

Yields below output.


# 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


mport 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)

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

References