Skip to content

Instantly share code, notes, and snippets.

@ameliagreenhall
Forked from mikedewar/df2json.py
Created February 4, 2013 18:56
Show Gist options
  • Save ameliagreenhall/4708718 to your computer and use it in GitHub Desktop.
Save ameliagreenhall/4708718 to your computer and use it in GitHub Desktop.
"""
tiny script to convert a pandas data frame into a JSON object
"""
import ujson as json
import pandas
import numpy as np
df = pandas.DataFrame({
"time" : [1,2,3,4,5],
"temp" : np.random.rand(5)
})
d = [
dict([
(colname, row[i])
for i,colname in enumerate(df.columns)
])
for row in df.values
]
return json.dumps(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment