pyspark.sql.functions.to_binary(col: ColumnOrName, format: Optional[ColumnOrName] = None) → pyspark.sql.column.Column[source]

Converts the input col to a binary value based on the supplied format. The format can be a case-insensitive string literal of “hex”, “utf-8”, “utf8”, or “base64”. By default, the binary format for conversion is “hex” if format is omitted. The function returns NULL if at least one of the input parameters is NULL.

New in version 3.5.0.

colColumn or str

Input column or strings.

formatColumn or str, optional

format to use to convert binary values.


>>> df = spark.createDataFrame([("abc",)], ["e"])
>>>, lit("utf-8")).alias('r')).collect()
>>> df = spark.createDataFrame([("414243",)], ["e"])