Pyspark n00b ...如何用自身的子串替换列?我正在尝试从字符串的开头和结尾删除选定数量的字符 .
from pyspark.sql.functions import substring
import pandas as pd
pdf = pd.DataFrame({'COLUMN_NAME':['_string_','_another string_']})
# this is what i'm looking for...
pdf['COLUMN_NAME_fix']=pdf['COLUMN_NAME'].str[1:-1]
df = sqlContext.createDataFrame(pdf)
# following not working... COLUMN_NAME_fix is blank
df.withColumn('COLUMN_NAME_fix', substring('COLUMN_NAME', 1, -1)).show()
这非常接近但略有不同Spark Dataframe column with last character of other column . 然后有这个LEFT and RIGHT function in PySpark SQL
2 回答
在你的代码中,
试试这个,(使用固定语法)
尝试:
其中1 =字符串中的起始位置,10 =从起始位置(包括)开始包含的字符数