Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
Convert the column to a different data type.
Syntax
cast(dataType)
Parameters
| Parameter | Type | Description |
|---|---|---|
dataType |
DataType or str | Target data type |
Returns
Column
Examples
Cast with a string type name:
from pyspark.sql.types import StringType
df = spark.createDataFrame(
[(2, "Alice"), (5, "Bob")], ["age", "name"])
df.select(df.age.cast("string").alias('ages')).collect()
# [Row(ages='2'), Row(ages='5')]
Cast with a DataType instance:
from pyspark.sql.types import StringType
df = spark.createDataFrame(
[(2, "Alice"), (5, "Bob")], ["age", "name"])
df.select(df.age.cast(StringType()).alias('ages')).collect()
# [Row(ages='2'), Row(ages='5')]