Pandas Series.fillna() Function

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:December 28, 2022

Pandas series.fillna() function 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 syntax of pandas.Series.fillna() function, its parameters and explain how to fill the NA/NaN/None missing values with a specified given value with examples.

1. Quick Examples of Series.fillna() Function

If you are in a hurry, below are some quick examples of the Pandas Series fillna() function.


# Below are quick examples

# 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')

2. Syntax of Series.fillna() Function

Following is the syntax of creating Series.fillna() function.


# Syntax of Series.fillna() function
Series.fillna(value=None, *, method=None, axis=None, inplace=False, limit=None, downcast=None)

2.1 Parameter of fillna()

Following are the parameters of the fillna().

  • value – Takes either scalar, dict, Series, or DataFrame but not list.
  • method – Takes one of these values {‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}. Default None.
  • 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 – bool, default False. When used True, fill in place. It updates the existing Series object.
  • limit – Specify how many fills should happen. This is the maximum number of consecutive NaN values replaced with the specified value.
  • downcast – It takes a dict of key-value pair that specifies the data type to downcast. Like Float64 to int64, date to string e.t.c

2.2 Return Value of fillna()

It returns a series of objects with fill missing values.

3. Initialize Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure available in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.

Now, let’s create pandas 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

4. 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

5. 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

6. 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

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

8. Conclusion

In this article, I have explained 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 e.t.c.

Happy Learning !!

Related Articles

References

Leave a Reply