我正在使用 pyspark 讀取 csv 檔案,如下所示:df = spark.read.format('csv').options(header=True, encoding='windows-1251',delimiter=';').load('csv_file.csv')
在列的結果中,我得到帶有“'”單引號字符的字串,像這樣12435'
檔案中沒有一行結尾有引號,idk 是 spark 找到它的地方
我需要洗掉此報價
順便說一句,pandas 在每行末尾讀取 csv 時沒有參考,但我無法將 pd.DF 轉換為 spark.DF,出現錯誤cannot merge type DoubleType and StringType
DF 有一些空列
我試過了:
from pyspark.sql.functions import *
for i in df.columns:
df.withColumn(i, expr("substring({name}, 1, length({name}) -1)".format(name=i)))
for i in df.columns:
df.withColumn(i, col(i).substr(lit(0), length(col(i)) - 1))
這些都沒有幫助我
泰
讀df
col1 | col2
12345' abcde'
預期產出
col1 | col2
12345 abcde
uj5u.com熱心網友回復:
使用串列理解
df.select(*[regexp_replace(F.col(c),"'",'').alias(c) for c in df.columns]).show()
----- -----
| col1| col2|
----- -----
|12345|abcde|
----- -----
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/457307.html
