pyspark.sql.functions.startswith¶
-
pyspark.sql.functions.
startswith
(str: ColumnOrName, prefix: ColumnOrName) → pyspark.sql.column.Column[source]¶ Returns a boolean. The value is True if str starts with prefix. Returns NULL if either input expression is NULL. Otherwise, returns False. Both str or prefix must be of STRING or BINARY type.
New in version 3.5.0.
Examples
>>> df = spark.createDataFrame([("Spark SQL", "Spark",)], ["a", "b"]) >>> df.select(startswith(df.a, df.b).alias('r')).collect() [Row(r=True)]
>>> df = spark.createDataFrame([("414243", "4142",)], ["e", "f"]) >>> df = df.select(to_binary("e").alias("e"), to_binary("f").alias("f")) >>> df.printSchema() root |-- e: binary (nullable = true) |-- f: binary (nullable = true) >>> df.select(startswith("e", "f"), startswith("f", "e")).show() +----------------+----------------+ |startswith(e, f)|startswith(f, e)| +----------------+----------------+ | true| false| +----------------+----------------+