【问题标题】:How to convert any datatype other than string to string in pyspark dataframe如何将字符串以外的任何数据类型转换为pyspark数据框中的字符串
【发布时间】:2018-02-02 17:26:31
【问题描述】:

我正在尝试对两个数据帧中的每一行应用 pyspark sql 函数哈希算法来识别差异。哈希算法基于字符串,所以我试图将字符串以外的任何数据类型转换为字符串。我在日期列转换中遇到了大部分问题,因为在转换为字符串之前需要更改日期格式以使其与基于哈希的匹配保持一致。请帮助我解决这个问题。

#Identify the fields which are not strings
from pyspark.sql.types import *
fields = df_db1.schema.fields
nonStringFields = map(lambda f: col(f.name), filter(lambda f: not isinstance(f.dataType, StringType), fields))

#Convert the date fields to specific date format and convert to string.
DateFields = map(lambda f: col(f.name), filter(lambda f: isistance(f.dataType, DateType), fields))

#convert all other fields other than string to string.

【问题讨论】:

    标签: python-3.x apache-spark pyspark spark-dataframe pyspark-sql


    【解决方案1】:

    对于数字和日期字段,您可以使用cast

    #filter rows
    DateFields = filter(lambda f: isinstance(f.dataType, DateType), fields)
    
    # cast to string
    dateFieldsWithCast = map(lambda f: col(f).cast("string").as(f.name), DateFields)
    

    以类似的方式,您可以创建具有 Long 类型等的列列表,然后执行 select 就像在 this 回答中一样

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 2022-01-13
      相关资源
      最近更新 更多