Python | Pandas Index.fillna ()

Index.fillna() Pandas Index.fillna() fills the NA / NaN values ​​with the specified value. Only a scalar value needs to be filled for all missing values ​​present in the index. The function returns a new object in which the missing values ​​are filled with the passed value.

Syntax: Index.fillna (value = None, downcast = None)

Parameters:
value: Scalar value to use to fill holes (eg 0). This value cannot be a list-likes.
downcast: a dict of item- & gt; dtype of what to downcast if possible, or the string `infer` which will try to downcast to an appropriate equal type (eg float64 to int64 if possible)

Returns: filled:% (klass) s

Example # 1: Use Index.fillna () to fill in any missing values ​​in the index.

# import pandas as pd

import pandas as pd

 
# Create index

idx = pd.Index ([ 1 , 2 , 3 , 4 , 5 , None , 7 , 8 , 9 , None ])

  
# Print index
idx

Output:

Let`s fill in any missing values ​​in the index -1.

# fill in the values ​​-1

idx.fillna ( - 1 )

Output:

As we can see in the output, Index.fillna () filled in all missing values ​​-1. The function only accepts a scalar value.

Example # 2: Use Index.fillna () to fill in any missing lines in the index.

# import pandas as pd

import pandas as pd

 
# Create index

idx = pd.Index ([ `Labrador` , ` Beagle` , None , `Labrador`

`Lhasa` , `Husky` , ` Beagle` , None , `Koala` ])

  
# Print index
idx

Output:

As we can see in the output, we have some missing values … For data analysis purposes, we would like to fill in these missing values ​​with some other indicative values ​​that serve our purpose.

# Fill in missing values ​​with & # 39; Value_Missing & # 39;

idx.fillna ( `Value_Missing` )

Output:

As we can see in the output, all missing rows in the index have been filled with the passed values.