亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將以下 json 文件轉換為數據框?

如何將以下 json 文件轉換為數據框?

狐的傳說 2023-02-22 13:55:48
我是 Python 新手,不知道如何將以下 JSON 數據轉換為 CSV 格式。非常感謝任何可以提供幫助的人!JSON數據是:{ "Hotel": [  {   "name": "Concorde Hotel New York",   "Reviewer": [    {     "name": "Serje J",     "Title": "Didn't make it but hope to soon",     "Title_url": "https://www.tripadvisor.com/ShowUserReviews-g60763-d14040955-r751642278-Concorde_Hotel_New_York-New_York_City_New_York.html",     "Description": "I was scheduled to stay here in March which unfortunately didn't happen due to COVID-19. I was forced to cancel my arrangements, however I was so impressed by the courtesy and promptitude with which Concorde staff arranged my cancellation and refund. When all this madness is behind us I will certainly be rebooking with this hotel.",     "Location": "Melbourne, Australia",     "Date": "Date of stay: March 2020",     "Rating": "ui_bubble_rating bubble_50"    },    {     "name": "John Schueler",     "Title": "Beautiful Modern Rooms in East MidTown",     "Title_url": "https://www.tripadvisor.com/ShowUserReviews-g60763-d14040955-r751405066-Concorde_Hotel_New_York-New_York_City_New_York.html",     "Description": "The lobby is under construction but the rooms are fresh, modern and very nicely appointed. The bathrooms are large and very nice. Only complaint was the wifi was weak in the room - which could be due to the construction disruption. Great location for sites and restaurants. Room was very quiet!",     "Location": "Beaufort, South Carolina",     "Date": "Date of stay: March 2020",     "Rating": "ui_bubble_rating bubble_40"    }   ]  } ]}
查看完整描述

2 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

  1. json_normalize將 pandas 中的記錄展平/結構化

  2. 為了美化,將Hotel.name移到第一列

  3. 使用 pandas 功能輸出為 CSV

import pandas as pd

js = {

 "Hotel": [

  {

   "name": "Concorde Hotel New York",

   "Reviewer": [

    {

     "name": "Serje J",

     "Title": "Didn't make it but hope to soon",

     "Title_url": "https://www.tripadvisor.com/ShowUserReviews-g60763-d14040955-r751642278-Concorde_Hotel_New_York-New_York_City_New_York.html",

     "Description": "I was scheduled to stay here in March which unfortunately didn't happen due to COVID-19. I was forced to cancel my arrangements, however I was so impressed by the courtesy and promptitude with which Concorde staff arranged my cancellation and refund. When all this madness is behind us I will certainly be rebooking with this hotel.",

     "Location": "Melbourne, Australia",

     "Date": "Date of stay: March 2020",

     "Rating": "ui_bubble_rating bubble_50"

    },

    {

     "name": "John Schueler",

     "Title": "Beautiful Modern Rooms in East MidTown",

     "Title_url": "https://www.tripadvisor.com/ShowUserReviews-g60763-d14040955-r751405066-Concorde_Hotel_New_York-New_York_City_New_York.html",

     "Description": "The lobby is under construction but the rooms are fresh, modern and very nicely appointed. The bathrooms are large and very nice. Only complaint was the wifi was weak in the room - which could be due to the construction disruption. Great location for sites and restaurants. Room was very quiet!",

     "Location": "Beaufort, South Carolina",

     "Date": "Date of stay: March 2020",

     "Rating": "ui_bubble_rating bubble_40"

    }

   ]

  }

 ]

}

# use provided functionality to flatten and normalise data

df = pd.json_normalize(js, record_path=["Hotel","Reviewer"], meta=[["Hotel","name"]])

# Hotel.name logically should be 1st column, move it

df.insert(0, "Hotel.name", df.pop("Hotel.name"))

# make it a CSV....

df.to_csv(index=False)


查看完整回答
反對 回復 2023-02-22
?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

首先,我建議您始終將 Json 粘貼到查看器中。這樣您就可以輕松地可視化文件結構。


然后,將您提供的示例保存為file.json,只需幾行代碼即可獲得:


import pandas as pd


file = pd.read_json("file.json")


temp = file['Hotel'][0]['Reviewer']


df = pd.DataFrame.from_dict(temp)


df['hotel'] = file['Hotel'][0]['name']


>> df


            name  ...                    hotel

0        Serje J  ...  Concorde Hotel New York

1  John Schueler  ...  Concorde Hotel New York


查看完整回答
反對 回復 2023-02-22
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號