In versions of Pandas > 0.19.0, DataFrame.to_json has a parameter, lines, that will write out JSONL format.
Given that, a more succinct version of your solution might look like this:
import pandas as pd
data = [{'label': 'DRUG', 'pattern': 'aspirin'},
{'label': 'DRUG', 'pattern': 'trazodone'},
{'label': 'DRUG', 'pattern': 'citalopram'}]
df = pd.DataFrame(data)
# Wrap pattern column in a dictionary
df["pattern"] = df.pattern.apply(lambda x: {"lower": x})
# Output in JSONL format
print(df.to_json(orient='records', lines=True))
Output:
{"label":"DRUG","pattern":{"lower":"aspirin"}}
{"label":"DRUG","pattern":{"lower":"trazodone"}}
{"label":"DRUG","pattern":{"lower":"citalopram"}}
Answer from kmsquire on Stack OverflowIn versions of Pandas > 0.19.0, DataFrame.to_json has a parameter, lines, that will write out JSONL format.
Given that, a more succinct version of your solution might look like this:
import pandas as pd
data = [{'label': 'DRUG', 'pattern': 'aspirin'},
{'label': 'DRUG', 'pattern': 'trazodone'},
{'label': 'DRUG', 'pattern': 'citalopram'}]
df = pd.DataFrame(data)
# Wrap pattern column in a dictionary
df["pattern"] = df.pattern.apply(lambda x: {"lower": x})
# Output in JSONL format
print(df.to_json(orient='records', lines=True))
Output:
{"label":"DRUG","pattern":{"lower":"aspirin"}}
{"label":"DRUG","pattern":{"lower":"trazodone"}}
{"label":"DRUG","pattern":{"lower":"citalopram"}}
Very short code that should work for easy coping-pasting.
output_path = "/data/meow/my_output.jsonl"
with open(output_path, "w") as f:
f.write(df_result.to_json(orient='records', lines=True, force_ascii=False))
If you are using jupyter notebook, you should use with open(output_path, "w") as f instead of f = open(output_path, "w") to make sure file is saved (correctly close) and ready to read in next cell.
Videos
The pd.read_json() function in the pandas library is used to read JSON data into a DataFrame. When reading a JSON Lines (JSONL) file, where each line represents a separate JSON object, you can use the lines=True parameter to properly parse the file, treating each line in the file as a separate JSON object.
df = pd.read_json("test.jsonl", lines=True)
If the file is large, you can also pass the chunksize to manipulate it in chunks.
This medium article provides a fairly simple answer, which can be adapted to be even shorter. All you need to do is read each line then parse each line with json.loads(). Like this:
import json
import pandas as pd
lines = []
with open(r'test.jsonl') as f:
lines = f.read().splitlines()
line_dicts = [json.loads(line) for line in lines]
df_final = pd.DataFrame(line_dicts)
print(df_final)
As cgobat pointed out in a comment, the medium article adds a few extra unnecessary steps, which have been optimized in this answer.
In newer versions of pandas (0.20.0+, I believe), this can be done directly:
df.to_json('temp.json', orient='records', lines=True)
Direct compression is also possible:
df.to_json('temp.json.gz', orient='records', lines=True, compression='gzip')
The output that you get after DF.to_json is a string. So, you can simply slice it according to your requirement and remove the commas from it too.
out = df.to_json(orient='records')[1:-1].replace('},{', '} {')
To write the output to a text file, you could do:
with open('file_name.txt', 'w') as f:
f.write(out)