目标: 对于具有模式的数据框
id:string
Cold:string
Medium:string
Hot:string
IsNull:string
annual_sales_c:string
average_check_c:string
credit_rating_c:string
cuisine_c:string
dayparts_c:string
location_name_c:string
market_category_c:string
market_segment_list_c:string
menu_items_c:string
msa_name_c:string
name:string
number_of_employees_c:string
number_of_rooms_c:string
Months In Role:integer
Tenured Status:string
IsCustomer:integer
units_c:string
years_in_business_c:string
medium_interactions_c:string
hot_interactions_c:string
cold_interactions_c:string
is_null_interactions_c:string
Run Code Online (Sandbox Code Playgroud)
我想添加一个新列,它是列的所有键和值的 JSON 字符串。我在这篇文章PySpark - Convert to JSON row by row和相关问题中使用了该方法。我的代码
df = df.withColumn("JSON",func.to_json(func.struct([df[x] for x in small_df.columns])))
Run Code Online (Sandbox Code Playgroud)
我有一个问题:
问题: 当任何行的列具有空值(并且我的数据有很多...)时,Json 字符串不包含键。即,如果 27 列中只有 9 列有值,那么 JSON 字符串只有 9 个键...我想要做的是维护所有键,但对于空值只需传递一个空字符串“”
有小费吗?