I am trying to understand how to convert a URL that consists of JSON, to a dataframe. I'm testing this sample code.
import requests
r = requests.get('https://www.chsli.org/sites/default/files/transparency/111888924_GoodSamaritanHospitalMedicalCenter_standardcharges.json')
print(r.json())
That gives me this.
{"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
Now, if I try t throw everything into a dataframe, like this...
df = pd.read_json(r.json(), orient='index')
print(df.head())
I am getting this error:
NameError: name 'df' is not defined
I think there may be a customized way of doing this, but I am not sure. How can I convert this JSON into a dataframe? Are there different ways to do this based on different scenarios of how JSON is structured? Thanks!!
CodePudding user response:
I read this: "Pandas read_json() accepts a 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)