Pandas DataFrame.fillna()

We can use the fillna() function to fill the null values in the dataset.

Syntax:

Parameters:

  • value: It is a value that is used to fill the null values, alternately a Series/dict/DataFrame.
  • method: A method that is used to fill the null values in the reindexed Series.
  • axis: It takes int or string value for rows/columns. Axis along which we need to fill missing values.
  • inplace: If it is True, it fills values at an empty place.
  • limit: It is an integer value that specifies the maximum number of consecutive forward/backward NaN value fills.
  • downcast: It takes a dict that specifies what to downcast like Float64 to int64.

Returns:

It returns an object in which the missing values are being filled.

Example1:

Output

       x
0     10.0
1     20.0
2     30.0
3     40.0
4     50.0
5     NaN
       x
0     10.0
1     20.0
2     30.0
3     40.0
4     50.0
5      0.0

Example2:

The below code is responsible for filling the DataFrame that consist some NaN values.

Output

    A    B     C    D
0  NaN  NaN   20.0  0
1  1.0  NaN   4.0   1
2  NaN  NaN   NaN   5
3  NaN  20.0  NaN   2

Example3:

In below code, we have used the fillna function to fill in some of the NaN values only.

Output

    A    B     C    D
0  0.0  1.0   20.0  0
1  1.0  NaN   4.0   1
2  NaN  NaN   2.0   5
3  NaN  20.0  NaN   2





Latest Courses