Site icon Spark By {Examples}

Pandas Series.fillna() Function

pandas series fillna

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

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

Exit mobile version