Is there a standard way to convert JSON to Dataframe? Or do different cases require different solutions?

Question:

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?

Asked By: ASH

||

Answers:

pandas.read_json doc: https://pandas.pydata.org/docs/reference/api/pandas.read_json.html

Any valid string path is acceptable. The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. A local file could be: file://localhost/path/to/table.json.

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)
Answered By: GuiEpi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.