我在從Json檔案中讀取專案時遇到了一些問題,并且在將資料從該檔案中獲取到列中時遇到了一些問題。
{
"sample"/span>:[
{
"value":"Red"。
"id":"1"。
},
{
"value":"green"。
"id":"2"。
},
{
"value":"orange"。
"id":"3"。
}
],
"scientific_names":"Buxus microphylla"/span>。
"性別":"灌木"。
"例子":"夾竹桃"。
}
我想在一個資料框架中獲得這個JSON物件,就像
一樣 ------------------ ------------------- -------- ---------- --
| 樣本 | 科學名稱 | 性別 | 示例 | |
------------------ ------------------- -------- ---------- --
| 紅,綠,橙|Buxus microphylla|灌木|夾竹桃|||
------------------ ------------------- -------- ---------- --
| | | | | |
------------------ ------------------- -------- ---------- --
| | | | | |
誰能幫幫我?謝謝你!
uj5u.com熱心網友回復:
你可以簡單地傳遞一個適當的模式并選擇value列
# a.json。
# {...}你的完整json樣本。
schema = T.StructType([
T.StructField('s sample', T.ArrayType(T.StructType([
T.StructField('id', T.StringType()) 。
T.StructField('value', T.StringType())
]))),
T.StructField('scientific_names', T.StringType())。
T.StructField('gender', T.StringType())。
T.StructField('examples', T.StringType())。
])
(spark
.讀取
.json('a.json', schema=schema, multiLine=True)
.withColumn('s sample', F.col('s sample.value')
.show(10, False)
)
# output 10 .
# -------------------- ----------------- ------ --------
# |sample |scientific_names |gender|examples|>
# -------------------- ----------------- ------ --------
# |[Red, green, orange]|Buxus microphylla|bushes|Oleander|
# -------------------- ----------------- ------ --------
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/310733.html
標籤:
