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
  • 1,057
  • 3
  • 24
  • 43
  • 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

5 Answers5

2

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

Padfoot123
  • 1,057
  • 3
  • 24
  • 43
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
  • 22,092
  • 39
  • 79
  • 102
Learner
  • 33
  • 7
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
  • 25,419
  • 17
  • 47
  • 88
0

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

fuzzy-memory
  • 157
  • 8
0

Df.isEmpty would work for everyone starting from spark version 3.3.0. Earlier spark version might not support isEmpty method for pyspark.

Nikunj Kakadiya
  • 2,689
  • 2
  • 20
  • 35