Python 如何使用.str和.split将pandas代码转换为Pyspark
我使用pandas编写了以下代码:Python 如何使用.str和.split将pandas代码转换为Pyspark,python,pandas,apache-spark,pyspark,apache-spark-sql,Python,Pandas,Apache Spark,Pyspark,Apache Spark Sql,我使用pandas编写了以下代码: df['last_two'] = df['text'].str[-2:] df['before_hyphen'] = df['text'].str.split('-').str[0] df['new_text'] = df['before_hyphen'].astype(str) + "-" + df['last_two'].astype(str) 但当我在spark数据帧上运行它时,我得到以下错误: TypeError:startPos
df['last_two'] = df['text'].str[-2:]
df['before_hyphen'] = df['text'].str.split('-').str[0]
df['new_text'] = df['before_hyphen'].astype(str) + "-" + df['last_two'].astype(str)
但当我在spark数据帧上运行它时,我得到以下错误:
TypeError:startPos和length必须是相同的类型
我知道我可以将df转换成pandas,运行代码,然后将其转换回spark df,但我想知道是否有更好的方法?谢谢您可以尝试以下字符串函数:
import pyspark.sql.functions as F
df2 = df.withColumn(
'last_two', F.expr('substring(text, -2)')
).withColumn(
'before_hyphen', F.substring_index('text', '-', 1))
).withColumn(
'new_text', F.concat_ws('-', 'before_hyphen', 'last_two')
)