Currently pandas.DataFrame.to_json supports orient=records which yields a file of [{column -> value}, ... , {column -> value}] (as a single line). I'd like to request a slight variation of this format which is a single {column -> value} per line. This yields a similar format as PostgreSQL's row_to_json() function.
Example of the TPC-H region table using orient=records
[{"r_regionkey":0,"r_name":"AFRICA","r_comment":"lar deposits. blithely final packages cajole. regular waters are final requests. regular accounts are according to "},{"r_regionkey":1,"r_name":"AMERICA","r_comment":"hs use ironic, even requests. s"},{"r_regionkey":2,"r_name":"ASIA","r_comment":"ges. thinly even pinto beans ca"},{"r_regionkey":3,"r_name":"EUROPE","r_comment":"ly final courts cajole furiously final excuse"},{"r_regionkey":4,"r_name":"MIDDLE EAST","r_comment":"uickly special accounts cajole carefully blithely close requests. carefully final asymptotes haggle furiousl"}]
desired new format:
{"r_regionkey":0,"r_name":"AFRICA","r_comment":"lar deposits. blithely final packages cajole. regular waters are final requests. regular accounts are according to "}
{"r_regionkey":1,"r_name":"AMERICA","r_comment":"hs use ironic, even requests. s"}
{"r_regionkey":2,"r_name":"ASIA","r_comment":"ges. thinly even pinto beans ca"}
{"r_regionkey":3,"r_name":"EUROPE","r_comment":"ly final courts cajole furiously final excuse"}
{"r_regionkey":4,"r_name":"MIDDLE EAST","r_comment":"uickly special accounts cajole carefully blithely close requests. carefully final asymptotes haggle furiousl"}
Comment From: jreback
dupe of #9180, though I like your example data.
This would be a straightforward change in to_json, just pass another orient I think and implement in c.
pull-requests are welcome.
Comment From: bashtage
Isn't one just a regex replace away from the other?
Comment From: jorisvandenbossche
Unifiying the to_dict and to_json orients a bit would also be nice.
Comment From: WillAyd
This is possible via df.to_json(orient="records", lines=True)