0

I am trying to check if a dataframe is empty in Pyspark using below.

print(df.head(1).isEmpty)

But, I am getting an error

Attribute error: 'list' object has no attribute 'isEmpty'.

I checked if my object is really a dd using type(df) and it is class 'pyspark.sql.dataframe.Dataframe'

Padfoot123
  • 853
  • 1
  • 13
  • 26
  • Does this answer your question? [How to check if spark dataframe is empty?](https://stackoverflow.com/questions/32707620/how-to-check-if-spark-dataframe-is-empty) – ZygD Sep 17 '21 at 11:15

4 Answers4

1

When u do a head(1) it returns a list. So that’s the reason for your error.

You have to just do df.isEmpty().

ZygD
  • 10,844
  • 36
  • 65
  • 84
Learner
  • 33
  • 6
0

I used df.first() == None to evaluate if my spark dataframe is empty

Padfoot123
  • 853
  • 1
  • 13
  • 26
0

df.head(1) returns a list corresponding to the first row of df.

You can check if this list is empty "[ ]" using a bool type condition as in:

if df.head(1):
    print("there is something")
else:
    print("df is empty")

>>> 'df is empty'

Empty lists are implicity "False".

For better explanation, please head over to python docs.

Andronicus
  • 24,333
  • 17
  • 47
  • 82
0

Another way to do this would be to check if df.count()==0