PySpark DataFrame | fillna method
Start your free 7-days trial now!
PySpark DataFrame's fillna(~)
method replaces null
values with your specified value. We can also pick the columns to perform the fill.
Parameters
1. value
| int
or float
or string
or boolean
or dict
The value to fill the null
values with. For dict
, the key will be the column labels and the value will be the fill value for that column. If dict
is passed, then subset
is ignored.
2. subset
| string
or tuple
or list
| optional
The columns to consider for filling. By default, all columns that are of the same type as value
will be considered.
Return Value
A PySpark DataFrame (pyspark.sql.dataframe.DataFrame
).
Examples
Consider the following PySpark DataFrame:
df = spark.createDataFrame([["Alex", 25, None], [None, 30, 200], ["Cathy", None, 100]], ["name", "age", "salary"])
+-----+----+------+| name| age|salary|+-----+----+------+| Alex| 25| null|| null| 30| 200||Cathy|null| 100|+-----+----+------+
Filling missing values in entire PySpark DataFrame
To fill all missing values with 50
:
+-----+---+------+| name|age|salary|+-----+---+------+| Alex| 25| 50|| null| 30| 200||Cathy| 50| 100|+-----+---+------+
Here, notice how the null
value is intact in the name
column. This is because we passed in 50
for the value
argument, which is a number type. However, the column name
is a string type, and because of the mismatch in the data types, the null
value was not filled for name
column.
Filling missing values with different values for different columns
To fill the null
values in age
with 50
, and those in salary
in 300
:
+-----+---+------+| name|age|salary|+-----+---+------+| Alex| 25| 300|| null| 30| 200||Cathy| 50| 100|+-----+---+------+
Filling missing values with the same value for different columns
To fill null
values for the age
and salary
columns with 50
:
+-----+---+------+| name|age|salary|+-----+---+------+| Alex| 25| 50|| null| 30| 200||Cathy| 50| 100|+-----+---+------+
Filling missing values using values of another column
Unfortunately, the fillna(-)
method does not allow for imputing missing values with values of another column.
Consider the following PySpark DataFrame:
df = spark.createDataFrame([["Alex", 25, None], [None, 30, 200], ["Cathy", None, 100]], ["name", "age", "salary"])
+-----+----+------+| name| age|salary|+-----+----+------+| Alex| 25| null|| null| 30| 200||Cathy|null| 100|+-----+----+------+
To impute missing values in age
with values in salary
, we can use PySpark's when(-)
method:
+-----+---+------+| name|age|salary|+-----+---+------+| Alex| 25| null|| null| 30| 200||Cathy|100| 100|+-----+---+------+