Dynamically setting schema for spark.createDataFrame

核能气质少年 提交于 2019-12-24 22:34:33

问题


So I am trying to dynamically set the type of data in the schema.

I have seen the code schema = StructType([StructField(header[i], StringType(), True) for i in range(len(header))]) on stackoverflow

But how can I add change this into a conditional statement?

If header is in list1 then IntergerType, if in list2 then DoubleType, else StringType for example?


回答1:


A colleague answered this for me

schema = StructType([
    StructField(header[i], DateType(), True) 
      if header[i] in dateFields 
      else StructField(header[i], StringType(), True)
      for i in range(len(header))])


来源:https://stackoverflow.com/questions/59035055/dynamically-setting-schema-for-spark-createdataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!