cph*_*sto 13
使用format_string函数在开头填充零。
from pyspark.sql.functions import col, format_string
df = spark.createDataFrame([('123',),('1234',)],['number',])
df.show()
+------+
|number|
+------+
| 123|
| 1234|
+------+
Run Code Online (Sandbox Code Playgroud)
如果数字是string,请确保将其投射到integer.
df = df.withColumn('number_padded', format_string("%012d", col('number').cast('int')))
df.show()
+------+-------------+
|number|number_padded|
+------+-------------+
| 123| 000000000123|
| 1234| 000000001234|
+------+-------------+
Run Code Online (Sandbox Code Playgroud)
小智 10
有lpad功能。用 pad 将字符串列左填充到宽度 len。
from pyspark.sql.functions import lpad
df.select(lpad(df.ID, 12, '0').alias('s')).collect()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7280 次 |
| 最近记录: |