pyspark.sql.SparkSession.emptyDataFrame#

SparkSession.emptyDataFrame(schema)[source]#

Creates an empty DataFrame with the specified schema.

New in version 4.2.0.

Parameters
schemaStructType or str

a StructType or a DDL-formatted string that describes the schema.

Returns
DataFrame

An empty DataFrame with the specified schema.

Examples

Create an empty DataFrame with a StructType schema.

>>> from pyspark.sql.types import StructType, StructField, StringType, IntegerType
>>> schema = StructType([
...     StructField("name", StringType(), True),
...     StructField("age", IntegerType(), True)
... ])
>>> df = spark.emptyDataFrame(schema)
>>> df.printSchema()
root
 |-- name: string (nullable = true)
 |-- age: integer (nullable = true)
>>> df.count()
0

Create an empty DataFrame with a DDL-formatted string schema.

>>> df = spark.emptyDataFrame("name STRING, age INT")
>>> df.printSchema()
root
 |-- name: string (nullable = true)
 |-- age: integer (nullable = true)
>>> df.count()
0