我試圖了解如何將包含 JSON 的 URL 轉換為資料框。我正在測驗這個示例代碼。
import requests
r = requests.get('https://www.chsli.org/sites/default/files/transparency/111888924_GoodSamaritanHospitalMedicalCenter_standardcharges.json')
print(r.json())
這給了我這個。
{"name":"Good Samaritan Hospital Medical Center","tax_id":"11-1888924","code":"57320","code type":"cpt","code description":"Closure of abnormal drainage tract from bladder into vagina","payer":"humana - medicare advantage","patient_class":"O","gross charge":"23452.80","de-identified minimum negotiated charge":"769.90","payer-specific negotiated charge":"3154.88","de-identified maximum negotiated charge":"3154.88","discounted cash price":"4690.56"}
{"name":"Good Samaritan Hospital Medical Center","tax_id":"11-1888924","code":"57320","code type":"cpt","code description":"Closure of abnormal drainage tract from bladder into vagina","payer":"HEALTH FIRST","patient_class":"O","gross charge":"23452.80","de-identified minimum negotiated charge":"769.90","payer-specific negotiated charge":"769.90","de-identified maximum negotiated charge":"3154.88","discounted cash price":"4690.56"}
: 421
現在,如果我嘗試將所有內容都放入資料框中,就像這樣......
df = pd.read_json(r.json(), orient='index')
print(df.head())
我收到此錯誤:
NameError: name 'df' is not defined
我認為可能有一種定制的方式來做到這一點,但我不確定。如何將此 JSON 轉換為資料框?根據 JSON 結構的不同場景,是否有不同的方法來做到這一點?謝謝!!
uj5u.com熱心網友回復:
我讀到:“Pandas read_json() 接受一個 URL。”
import pandas as pd
df = pd.read_json("http://raw.githubusercontent.com/BindiChen/machine-learning/master/data-analysis/027-pandas-convert-json/data/simple.json")
print(df)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/453736.html
標籤:Python json python-3.x 数据框
