DataFrame.
to
Returns a new DataFrame where each row is reconciled to match the specified schema.
DataFrame
New in version 3.4.0.
Changed in version 3.4.0: Supports Spark Connect.
StructType
Specified schema.
Reconciled DataFrame.
Notes
Reorder columns and/or inner fields by name to match the specified schema.
Missing columns and/or inner fields (present in the specified schema but not input DataFrame) lead to failures.
if the types are compatible, e.g., numeric to numeric (error if overflows), but not string to int.
still keep their own metadata if not overwritten by the specified schema.
is nullable but the specified schema requires them to be not nullable.
Examples
>>> from pyspark.sql.types import StructField, StringType >>> df = spark.createDataFrame([("a", 1)], ["i", "j"]) >>> df.schema StructType([StructField('i', StringType(), True), StructField('j', LongType(), True)])
>>> schema = StructType([StructField("j", StringType()), StructField("i", StringType())]) >>> df2 = df.to(schema) >>> df2.schema StructType([StructField('j', StringType(), True), StructField('i', StringType(), True)]) >>> df2.show() +---+---+ | j| i| +---+---+ | 1| a| +---+---+