You have to use coalesce or NOT NULL to substitute null values in select query.

Check below if it helps:


Try this:

spark.sql("""
select
 patient_id,
 CASE 
 when cough is NOT NULL THEN cough
 else 0
 END as cough,
 CASE 
 when feaver is NOT NULL THEN feaver
 else 0
 END as feaver,
 CASE 
 when `head ache` is NOT NULL THEN `head ache`
 else 0
 END as `head ache`
 from ( 
select * from patient
)
PIVOT(
  Count(dx)
  for dx in ('cough','feaver','head ache')
)
;
""").show()

The output will be:

patient_id cough feaver head ache
Donna 1 0 1
Jerry 1 0 0
Bob 1 1 0

if you want it to be dynamic

dist=spark.sql("select collect_set(dx) from patient;").toPandas()
val=spark.sql("""
select
 patient_id,
 coalesce(cough,0) as `cough`,
 coalesce(feaver,0) as `feaver`,
 coalesce(`head ache`,0) as `head ache`
 from ( 
select * from patient
)
PIVOT(
  Count(dx)
  for dx in """
+
str(tuple(map(tuple, *dist.values))[0])
+
"""
)
;
""")
Answer from Saibal on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › databricks › sql › language-manual › functions › ifnull
ifnull function - Azure Databricks - Databricks SQL | Microsoft Learn
Applies to: Databricks SQL Databricks Runtime · Returns expr2 if expr1 is NULL, or expr1 otherwise. This function is a synonym for coalesce(expr1, expr2) with two arguments. ifnull(expr1, expr2) expr1: An expression of any type. expr2: An expression sharing a least common type with expr1.
🌐
Databricks
docs.databricks.com › reference › sql language reference › null semantics
NULL semantics | Databricks on AWS
> SELECT max(age) FROM person where 1 = 0; max(age) -------- null · WHERE, HAVING operators filter rows based on the user specified condition. A JOIN operator is used to combine rows from two tables based on a join condition. For all the three operators, a condition expression is a boolean expression and can return True, False or Unknown (NULL). They are “satisfied” if the result of the condition is True. SQL ·
🌐
CastorDoc
castordoc.com › how-to › how-to-use-ifnull-in-databricks
How to use ifnull in Databricks?
The syntax for using ifnull in Databricks is as follows:SELECT IFNULL(column_name, alternative_value) FROM table_name; Here, column_name represents the column in which you want to replace null values, and alternative_value refers to the value ...
Find elsewhere
🌐
Databricks
docs.databricks.com › reference › sql language reference › functions › built-in functions › alphabetical list of built-in functions › isnull function
isnull function | Databricks on AWS
the result is always false. Use the is_variant_null function function to check if the VARIANT encoded value is NULL, or cast the VARIANT to a specific type and check if the result is NULL.
🌐
Databricks
awsfw1-us.corp.databricks.com › home › 5 ways to replace null with 0 in sql
5 Ways to Replace Null with 0 in SQL - DataSpark Innovations
May 12, 2025 - The syntax for the COALESCE function ... 3: Using the IFNULL Function</h2> <p>The IFNULL function is similar to the ISNULL function and is used to replace null with 0 in SQL....
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › databricks › sql › language-manual › sql-ref-null-semantics
NULL semantics - Azure Databricks - Databricks SQL | Microsoft Learn
> SELECT * FROM person WHERE NOT EXISTS (SELECT null); name age ---- --- -- `NOT EXISTS` expression returns `TRUE`. > SELECT * FROM person WHERE NOT EXISTS (SELECT 1 WHERE 1 = 0); name age -------- ---- Albert null Michelle 30 Fred 50 Mike 18 Dan 50 Marry null Joe 30 · In Azure Databricks, IN and NOT IN expressions are allowed inside a WHERE clause of a query.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1464868 › isnull-in-databricks
isnull in databricks - Microsoft Q&A
The error message suggests that the isnull function is being called with two parameters instead of one. The isnull function in Spark SQL is used to check if a column is null or not.
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › databricks › sql › language-manual › functions › isnullop
is null operator - Azure Databricks - Databricks SQL | Microsoft Learn
the result of is null is always false. Use the is_variant_null function function to check if the VARIANT encoded value is NULL, or cast the VARIANT to a specific type and check if the result is NULL.
🌐
Databricks Community
community.databricks.com › t5 › data-engineering › unable-to-replace-null-with-0-in-dataframe-using-pyspark › td-p › 29590
unable to replace null with 0 in dataframe using Pyspark databricks notebook (community edition)
October 3, 2022 - from pyspark.sql.functions import col emp_csv_df = emp_csv_df.na.fill(0).withColumn("Total_Sal",col('sal')+col('comm')) display(emp_csv_df) ... I bet that it is not real null but the string "null".