gpt4 book ai didi

python - 将表列和值转换为嵌套 JSON

转载 作者:行者123 更新时间:2023-12-01 06:30:15 24 4
gpt4 key购买 nike

我有一个 CSV 文件,其中的列如下

enter image description here

嵌套 JSON 对象所需的结果如下:

{
"Type" : "A"
"Value" :"1"

}

{
"Type" : "B"
"Value" :"1"

}

我尝试了以下代码:任何帮助将不胜感激。

from pyspark.sql.functions import *

list1=['A','B','C']
df2=spark.sql("select * from test limit 10" )
df3=df2.select(list1)
for i in range(0,len(list1)):
df4=df3.withColumn("Type",lit(list1[i]))

最佳答案

这可以通过 UnPivoting 技巧来完成...

假设您有如下所示的数据集..让我们将其称为患者的测试结果..A、B、C 列表示..测试类型 A 、测试类型 B...以及这些列中的值表示数字形式的测试结果

+-------------+---+---+---+---+---+---+---+
|PatientNumber| A| B| C| D| E| F| G|
+-------------+---+---+---+---+---+---+---+
| 101| 1| 2| 3| 4| 5| 6| 7|
| 102| 11| 12| 13| 14| 15| 16| 17|
+-------------+---+---+---+---+---+---+---+

我添加了 PatientNumber 列,只是为了使数据看起来更合理。您可以从代码中删除它。

我将此数据集添加到 csv..

val testDF = spark.read.format("csv").option("header", "true").load("""C:\TestData\CSVtoJSon.csv""")

让我们创建 2 个数组,一个用于 id 列,另一个用于所有测试类型..

val idCols = Array("PatientNumber")

val valCols = testDF.columns.diff(idCols)

然后这是 Unpivot 的代码

val valcolNames = valCols.map(x => List(''' + x + ''', x))
val unPivotedDF = testDF.select($"PatientNumber", expr(s"""stack(${valCols.size},${valcolNames.flatMap(x => x).mkString(",")} ) as (Type,Value)"""))

以下是未透视数据的样子 -

+-------------+----+-----+
|PatientNumber|Type|Value|
+-------------+----+-----+
| 101| A| 1|
| 101| B| 2|
| 101| C| 3|
| 101| D| 4|
| 101| E| 5|
| 101| F| 6|
| 101| G| 7|
| 102| A| 11|
| 102| B| 12|
| 102| C| 13|
| 102| D| 14|
| 102| E| 15|
| 102| F| 16|
| 102| G| 17|
+-------------+----+-----+

最后将这个 Unpivoted DF 写为 Json -

unPivotedDF.coalesce(1).write.format("json").mode("Overwrite").save("""C:\TestData\output""")

Json 文件的内容看起来与您想要的结果相同 -

{"PatientNumber":"101","Type":"A","Value":"1"}
{"PatientNumber":"101","Type":"B","Value":"2"}
{"PatientNumber":"101","Type":"C","Value":"3"}
{"PatientNumber":"101","Type":"D","Value":"4"}
{"PatientNumber":"101","Type":"E","Value":"5"}
{"PatientNumber":"101","Type":"F","Value":"6"}
{"PatientNumber":"101","Type":"G","Value":"7"}
{"PatientNumber":"102","Type":"A","Value":"11"}
{"PatientNumber":"102","Type":"B","Value":"12"}
{"PatientNumber":"102","Type":"C","Value":"13"}
{"PatientNumber":"102","Type":"D","Value":"14"}
{"PatientNumber":"102","Type":"E","Value":"15"}
{"PatientNumber":"102","Type":"F","Value":"16"}
{"PatientNumber":"102","Type":"G","Value":"17"}

关于python - 将表列和值转换为嵌套 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59948692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com