Home > Enterprise >  Pandas to_json in separate lines
Pandas to_json in separate lines

Time:10-27

How can I get the json format from pandas, where each rows are separated with new line. For example if I have a dataframe like:

import pandas as pd

data = [{'a': 1, 'b': 2},
        {'a': 3, 'b': 4}]

df = pd.DataFrame(data)


print("First:\n", df.to_json(orient="records"))

print("Second:\n", df.to_json(orient="records", lines=True))

Output:

First:
 [{"a":1,"b":2},{"a":3,"b":4}]
Second:
 {"a":1,"b":2}
{"a":3,"b":4}

But I really want an output like so:

[{"a":1,"b":2},
{"a":3,"b":4}]

or

[
 {"a":1,"b":2},
 {"a":3,"b":4}
]

I really just want each line to be separated by new line, but still a valid json format that can be read. I know I can use to_json with lines=True and just split by new line then .join, but wondering if there is a more straight forward/faster solution just using pandas.

CodePudding user response:

Here you go:

import json

list(json.loads(df.to_json(orient="index")).values())

CodePudding user response:

Use indent parameter

import pandas as pd

data = [{'a': 1, 'b': 2},
        {'a': 3, 'b': 4}]

df = pd.DataFrame(data)

print(df.to_json(orient="records", indent=1))
#output :
[
 {
  "a":1,
  "b":2
 },
 {
  "a":3,
  "b":4
 }
]

CodePudding user response:

Why don't you just add the brackets:

print(f"First:\n[{df.to_json(orient='records', lines=True)}]")
print(f"Second:\n[\n{df.to_json(orient='records', lines=True)}\n]")
  • Related