Created
December 16, 2011 13:17
-
-
Save mikedewar/1486027 to your computer and use it in GitHub Desktop.
A little script to convert a pandas data frame to a JSON object. Is there a better way?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
Looks like thats comming in the next version.
http://pandas.pydata.org/pandas-docs/dev/io.html#writing-json
To complete the thread: Convert DataFrame to JSON with pandas.io.
and now since a little while just use:
df.to_json()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A related pull request:
pandas-dev/pandas#1263