我使用databricks spark-csv模块作为sqlContext读入csv文件。我自定义了我的架构,如下例所示。但是,我在我的数据中注意到第3列,它是汽车的模型,其中的字符串总是在它前面有一个共同的字符串“model:”。有没有办法修剪常见的字符串?
from pyspark.sql import SQLContext
from pyspark.sql.types import *
sqlContext = SQLContext(sc)
customSchema = StructType([ \
StructField("year", IntegerType(), True), \
StructField("make", StringType(), True), \
StructField("model", StringType(), True), \
StructField("comment", StringType(), True), \
StructField("blank", StringType(), True)])
df = sqlContext.read \
.format('com.databricks.spark.csv') \
.options(header='true') \
.load('cars.csv', schema = customSchema)
你可以使用regexp_replace
:
from pyspark.sql import functions as F
[...]
df = df.withColumn('model', F.regexp_replace(df.model, r'^model:', ''))