Check if a Dataframe is empty, python

38 Views Asked by At

I am trying to check if a Dataframe has data inside:

The code is:

df = spark.sql("SELECT * FROM pts_dev.data_quality.rpa where family = '{0}' and mm_aaaa_ref = '{1}'".format(family,mm_aaaa))
display(df)

if df is not None:
    print("no empty")
else:
    print("empty")

The df does not have any result (is empty), but the message I got is not empty. I think my error is when I define df, I have tried if (len)df is not None: but it says that Dataframe has no len()

Could you help me?

Thanks in advance

2

There are 2 best solutions below

1
user23287795 On BEST ANSWER
0
random On

You can also do:

if df.count() > 0: # no empty
    print("no empty")
else: # empty
    print("empty")