我有一个数据框df
,读取的JSON文件如下:
df = spark.read.json("/myfiles/file1.json")
df.dtypes
显示以下列和数据类型:
id – string
Name - struct
address - struct
Phone - struct
start_date - string
years_with_company - int
highest_education - string
department - string
reporting_hierarchy - struct
我想提取非struct类型的列并创建一个数据框。例如,我的结果数据框应该只有id
、start_date
、highest_education
和department
这些列。
以下是我部分可用的代码,因为我只得到了最后一个非struct类型的列department
的值。我想收集所有的非struct类型列并将其转换为数据框:
names = df.schema.names
for col_name in names:
if isinstance(df.schema[col_name].dataType, StructType):
print("Skipping struct column %s "%(col_name))
else:
df1 = df.select(col_name).collect()
我相信这可能不是最好的方法,而且我有一些想不出来的问题,所以我希望得到你的帮助。谢谢。