pyspark.sql.DataFrame.createTempView

DataFrame.createTempView(name: str) → None[source]

Creates a local temporary view with this DataFrame.

The lifetime of this temporary table is tied to the SparkSession that was used to create this DataFrame. throws TempTableAlreadyExistsException, if the view name already exists in the catalog.

New in version 2.0.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
namestr

Name of the view.

Examples

Create a local temporary view.

>>> df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], schema=["age", "name"])
>>> df.createTempView("people")
>>> df2 = spark.sql("SELECT * FROM people")
>>> sorted(df.collect()) == sorted(df2.collect())
True

Throw an exception if the table already exists.

>>> df.createTempView("people")  
Traceback (most recent call last):
...
AnalysisException: "Temporary table 'people' already exists;"
>>> spark.catalog.dropTempView("people")
True