我正在嘗試使用 .withColumn 去除列中字串的一部分
這是列中的值的方式:
df1["column1"] = ["Temp 1 (gen. comb.)", "Temp 1", "Temp 2 (gen. comb.)", "Temp 2","Temp 3 (gen. comb.)", "Temp 3"]
我想從列中洗掉值(gen.comb.)
我在 PySpark 中嘗試過的代碼:
result_df = res.withColumn('c_model_detail', F.regexp_replace('column1', '(gen. comb.)', ''))
但是當我嘗試上面的結果列時,如下所示:
result_df["column1"] = ["Temp 1 ()", "Temp 1", "Temp 2 ()", "Temp 2","Temp 3 ()", "Temp 3"]
誰能幫我解決這個問題?我寫的代碼有什么錯誤?
在熊貓中,我嘗試了這段代碼并且有效
result_df["column1"] = df["column1"].str.replace(" (gen. comb.)","",regex=False)
誰能告訴我如何使用Pyspark 剝離字串?
uj5u.com熱心網友回復:
由于您使用的是正則運算式替換,因此您需要轉義括號。
這應該有效:
result_df = df.withColumn('c_model_detail', regexp_replace('column1', ' \(gen\. comb\.\)', ''))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520251.html
標籤:细绳数据框pyspark
