pyspark.sql.DataFrame.schema#

property DataFrame.schema#

Returns the schema of this DataFrame as a pyspark.sql.types.StructType.

New in version 1.3.0.

Changed in version 3.4.0: Supports Spark Connect.

Returns
StructType

Examples

Example 1: Retrieve the inferred schema of the current DataFrame.

>>> df = spark.createDataFrame(
...     [(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> df.schema
StructType([StructField('age', LongType(), True),
            StructField('name', StringType(), True)])

Example 2: Retrieve the schema of the current DataFrame (DDL-formatted schema).

>>> df = spark.createDataFrame(
...     [(14, "Tom"), (23, "Alice"), (16, "Bob")],
...     "age INT, name STRING")
>>> df.schema
StructType([StructField('age', IntegerType(), True),
            StructField('name', StringType(), True)])

Example 3: Retrieve the specified schema of the current DataFrame.

>>> from pyspark.sql.types import StructType, StructField, StringType
>>> df = spark.createDataFrame(
...     [("a",), ("b",), ("c",)],
...     StructType([StructField("value", StringType(), False)]))
>>> df.schema
StructType([StructField('value', StringType(), False)])