Built with Tutify.io

    AIStudyLabs

    Pandas Series.fillna() Function

    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.

    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 –

    <a id="h-quick-examples-of-pandas-series-fillna"></a>

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

    <a id="h-pandas-series-fillna-introduction"></a>

    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)
    

    <a id="h-parameter-of-fillna"></a>

    Parameter of fillna()

    Following are the parameters of the fillna().

    <a id="h-return-value-of-fillna"></a>

    Return Value of fillna()

    It returns a series of objects with fill missing values.

    <a id="h-initialize-pandas-series"></a>

    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
    

    <a id="h-use-series-fillna-function"></a>

    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
    

    <a id="h-fill-value-for-all-nan"></a>

    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
    

    <a id="h-pandas-series-fillna-function-using-method-ffill"></a>

    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
    

    <a id="h-complete-example-for-series-fillna-function"></a>

    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)
    

    <a id="h-pandas-series-fillna-function"></a>

    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.

    <a id="h-conclusion"></a>

    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