Pandas Dataframe to JSON File with Separate Records

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2013-10-28T14:53:26Z Indexed on 2013/10/28 15:54 UTC
Read the original article Hit count: 819

Filed under:
|

I'm attempting to dump data from a Pandas Dataframe into a JSON file to import into MongoDB. The format I require in a file has JSON records on each line of the form:

{<column 1>:<value>,<column 2>:<value>,...,<column N>:<value>}

df.to_json(,orient='records') gets close to the result but all the records are dumped within a single JSON array.

Any thoughts on an efficient way to get this result from a dataframe?

UPDATE: The best solution I've come up with is the following:

dlist = df.to_dict('records')
dlist = [json.dumps(record)+"\n" for record in dlist]
open('data.json','w').writelines(dlist)

© Stack Overflow or respective owner

Related posts about python

Related posts about pandas