pyspark.sql.SparkSession.emptyDataFrame#
- SparkSession.emptyDataFrame(schema)[source]#
Creates an empty
DataFramewith the specified schema.New in version 4.2.0.
- Parameters
- schema
StructTypeor str a
StructTypeor a DDL-formatted string that describes the schema.
- schema
- Returns
DataFrameAn 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