【问题标题】:How to remove white spaces between the quotes in pyspark dataframe?如何删除pyspark数据框中引号之间的空格?
【发布时间】:2020-12-01 20:05:52
【问题描述】:

我正在尝试删除引号之间的空格,但没有得到正确的结果。你能帮我怎么做吗?

例子:

Local_Manufacturer|SKU_PackID_ProductNumber|Molecule_Name|BrandName_Intl
"UPJOHN                 "|"894265"|"SILDENAFIL"|"REVATIO"

理想的输出:

Local_Manufacturer|SKU_PackID_ProductNumber|Molecule_Name|BrandName_Intl
"UPJOHN"|"894265"|"SILDENAFIL"|"REVATIO"

我试过下面的代码:

for c_name in df1.columns:
     df1 = df1.withColumn(c_name, trim(df1[c_name]))

【问题讨论】:

标签: dataframe csv pyspark whitespace


【解决方案1】:

导入trim函数。

import pyspark.sql.functions as f

for c_name in df1.columns:
     df1 = df1.withColumn(c_name, f.trim(df1[c_name]))
        
df_list = df1.collect()
print(df_list)

[Row(Local_Manufacturer='UPJOHN', SKU_PackID_ProductNumber='894265', Molecule_Name='SILDENAFIL', BrandName_Intl='REVATIO')]

结果被修剪。

【讨论】:

    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    相关资源
    最近更新 更多