I found a quick and easy solution to what I wanted using json_normalize() included in pandas 1.01.
from urllib2 import Request, urlopen
import json
import pandas as pd
path1 = '42.974049,-81.205203|42.974298,-81.195755'
request=Request('http://maps.googleapis.com/maps/api/elevation/json?locations='+path1+'&sensor=false')
response = urlopen(request)
elevations = response.read()
data = json.loads(elevations)
df = pd.json_normalize(data['results'])
This gives a nice flattened dataframe with the json data that I got from the Google Maps API.
Answer from pbreach on Stack OverflowI found a quick and easy solution to what I wanted using json_normalize() included in pandas 1.01.
from urllib2 import Request, urlopen
import json
import pandas as pd
path1 = '42.974049,-81.205203|42.974298,-81.195755'
request=Request('http://maps.googleapis.com/maps/api/elevation/json?locations='+path1+'&sensor=false')
response = urlopen(request)
elevations = response.read()
data = json.loads(elevations)
df = pd.json_normalize(data['results'])
This gives a nice flattened dataframe with the json data that I got from the Google Maps API.
Check this snip out.
# reading the JSON data using json.load()
file = 'data.json'
with open(file) as train_file:
dict_train = json.load(train_file)
# converting json dataset from dictionary to dataframe
train = pd.DataFrame.from_dict(dict_train, orient='index')
train.reset_index(level=0, inplace=True)
Hope it helps :)
Videos
In newer versions of pandas (0.20.0+, I believe), this can be done directly:
df.to_json('temp.json', orient='records', lines=True)
Direct compression is also possible:
df.to_json('temp.json.gz', orient='records', lines=True, compression='gzip')
The output that you get after DF.to_json is a string. So, you can simply slice it according to your requirement and remove the commas from it too.
out = df.to_json(orient='records')[1:-1].replace('},{', '} {')
To write the output to a text file, you could do:
with open('file_name.txt', 'w') as f:
f.write(out)
Suppose that I have the following dataframe:
info drinks reviews score menu funfacts
input 1.0 1 2 4 1. funfacts, 2. fun fact, 3. fun fact
How could I transform this to the required JSON format? I tried Pandas(df.to_json) however the default formatting seems incorrect.
Snippet: `df3.to_json('File Name.json', orient='records')`
Expected output:
{
"info":[
{
"drinks":[
"1.0"
],
"reviews":[
"1"
],
"score":[
"2"
],
"menu":[
"4"
],
"funfacts":[
"1. funfacts",
"2. fun fact",
"3. fun fact"
]
}
]
}
Current output:
[
{
"drinks": "1.0",
"reviews": "1",
"score": "2",
"menu": "4",
"funfacts": "1. funfacts ,2. fun facts ,3 fun facts"
}
]
Are there any arguments in pandas that I could use to get the desired format or do I need to use a different solution? Thanks
I need to convert a list of JSON strings into a dataframe. How do I do that?