<p>这里不需要自定义项。<code>Column</code>已经为<a href="https://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.Column.cast" rel="noreferrer">^{<cd2>} method</a>提供了<a href="https://spark.apache.org/docs/latest/api/python/pyspark.sql.html?highlight=datatype#pyspark.sql.types.DataType" rel="noreferrer">^{<cd3>}</a><em>实例:</p>
<pre class="lang-python prettyprint-override"><code>from pyspark.sql.types import DoubleType
changedTypedf = joindf.withColumn("label", joindf["show"].cast(DoubleType()))
</code></pre>
<p>或短字符串:</p>
<pre class="lang-python prettyprint-override"><code>changedTypedf = joindf.withColumn("label", joindf["show"].cast("double"))
</code></pre>
<p>其中规范字符串名称(也可以支持其他变体)对应于<code>simpleString</code>值。对于原子类型:</p>
<pre><code>from pyspark.sql import types
for t in ['BinaryType', 'BooleanType', 'ByteType', 'DateType',
'DecimalType', 'DoubleType', 'FloatType', 'IntegerType',
'LongType', 'ShortType', 'StringType', 'TimestampType']:
print(f"{t}: {getattr(types, t)().simpleString()}")
</code></pre>
<pre class="lang-none prettyprint-override"><code>BinaryType: binary
BooleanType: boolean
ByteType: tinyint
DateType: date
DecimalType: decimal(10,0)
DoubleType: double
FloatType: float
IntegerType: int
LongType: bigint
ShortType: smallint
StringType: string
TimestampType: timestamp
</code></pre>
<p>例如复杂类型</p>
<pre class="lang-py prettyprint-override"><code>types.ArrayType(types.IntegerType()).simpleString()
</code></pre>
<pre class="lang-none prettyprint-override"><code>'array<int>'
</code></pre>
<pre class="lang-py prettyprint-override"><code>types.MapType(types.StringType(), types.IntegerType()).simpleString()
</code></pre>
<pre class="lang-none prettyprint-override"><code>'map<string,int>'
</code></pre>