Change language

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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically