问题
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