Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pySpark check if dataframe exists

Is there a way to check if a dataframe exists in pySpark?

I know in native python, to check if dataframe exists:

exists(df_name) && is.data.frame(get(df_name))

How can this be done in pySpark? Since command exists throws an error.

like image 492
jgtrz Avatar asked Dec 22 '22 17:12

jgtrz


2 Answers

It is same as Petel code. You can import the dataframe type.

 from pyspark.sql import DataFrame

 df= sc.parallelize([
 (1,2,3), (4,5,7)]).toDF(["a", "b", "c"])

 if df is not None and isinstance(df,DataFrame):
      #<some operation>
      print("dataframe exists")
like image 89
kites Avatar answered Jan 03 '23 05:01

kites


try this: df_name is not None and isinstance(df_name, DataFrame)

like image 32
Idan Petel Avatar answered Jan 03 '23 04:01

Idan Petel