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 Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas 3.0.1 documentation
orient='table' contains a ‘pandas_version’ field under ‘schema’. This stores the version of pandas used in the latest revision of the schema. ... >>> from json import loads, dumps >>> df = pd.DataFrame( ... [["a", "b"], ["c", "d"]], ... index=["row 1", "row 2"], ... columns=["col 1", "col 2"], ... ) >>> result = df.to_json(orient="split") >>> parsed = loads(result) >>> dumps(parsed, indent=4) {{ "columns": [ "col 1", "col 2" ], "index": [ "row 1", "row 2" ], "data": [ [ "a", "b" ], [ "c", "d" ] ] }}
🌐
OpenAI Developer Community
community.openai.com › documentation
Convert Pandas dataframe to jsonl - Documentation - OpenAI Developer Community
June 21, 2021 - Hi guys, I have a table that looks like this Feedback content 1 content 2 content 3 … How do I convert this to the jsonl in this format using pandas: {text: content 1} {text:content 2} as required by the specia…
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_json.html
pandas.read_json — pandas 3.0.1 documentation - PyData |
The encoding to use to decode py3 bytes. encoding_errorsstr, optional, default “strict” · How encoding errors are treated. List of possible values . ... Read the file as a json object per line.
🌐
W3Schools
w3schools.com › python › pandas › pandas_json.asp
Pandas Read JSON
import pandas as pd data = { "Duration":{ "0":60, "1":60, "2":60, "3":45, "4":45, "5":60 }, "Pulse":{ "0":110, "1":117, "2":103, "3":109, "4":117, "5":102 }, "Maxpulse":{ "0":130, "1":145, "2":135, "3":175, "4":148, "5":127 }, "Calories":{ "0":409, "1":479, "2":340, "3":282, "4":406, "5":300 } } df = pd.DataFrame(data) print(df) Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
NVIDIA Developer
developer.nvidia.com › blog › json-lines-reading-with-pandas-100x-faster-using-nvidia-cudf
JSON Lines Reading with pandas 100x Faster Using NVIDIA cuDF | NVIDIA Technical Blog
April 23, 2025 - To use information from JSON Lines in data processing pipelines, the tokens must often be converted into a Dataframe or columnar format, such as Apache Arrow. JSON readers, such as pandas.read_json convert input character data into a Dataframe organized by columns and rows.
🌐
GeeksforGeeks
geeksforgeeks.org › python › exporting-pandas-dataframe-to-json-file
Exporting Pandas DataFrame to JSON File - GeeksforGeeks
February 18, 2026 - This example demonstrates how to create a small DataFrame with three rows and three columns and save it as a JSON file. ... import pandas as pd df = pd.DataFrame([['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], index=['row 1', 'row 2', 'row 3'], columns=['col 1', 'col 2', 'col 3']) df.to_json('file.json', orient='split', compression='infer', index=True) df = pd.read_json('file.json', orient='split', compression='infer') print(df)
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.0 › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas 2.0.3 documentation
orient='table' contains a ‘pandas_version’ field under ‘schema’. This stores the version of pandas used in the latest revision of the schema. ... >>> from json import loads, dumps >>> df = pd.DataFrame( ... [["a", "b"], ["c", "d"]], ... index=["row 1", "row 2"], ... columns=["col 1", "col 2"], ... ) >>> result = df.to_json(orient="split") >>> parsed = loads(result) >>> dumps(parsed, indent=4) { "columns": [ "col 1", "col 2" ], "index": [ "row 1", "row 2" ], "data": [ [ "a", "b" ], [ "c", "d" ] ] }
Find elsewhere
🌐
Spark Code Hub
sparkcodehub.com › pandas › data-export › to-json-guide
Converting Pandas DataFrame to JSON: A Comprehensive Guide
Validation: Parsed JSON to ensure correctness before sending. API Integration: Prepared data for a POST request (mocked for illustration). For more on datetime handling, see Pandas Time Series. For large DataFrames or frequent conversions, consider these optimizations: Use JSON Lines: lines=True reduces memory usage for large datasets.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.to_json.html
pandas.Series.to_json — pandas 3.0.1 documentation
orient='table' contains a ‘pandas_version’ field under ‘schema’. This stores the version of pandas used in the latest revision of the schema. ... >>> from json import loads, dumps >>> df = pd.DataFrame( ... [["a", "b"], ["c", "d"]], ... index=["row 1", "row 2"], ... columns=["col 1", "col 2"], ... ) >>> result = df.to_json(orient="split") >>> parsed = loads(result) >>> dumps(parsed, indent=4) {{ "columns": [ "col 1", "col 2" ], "index": [ "row 1", "row 2" ], "data": [ [ "a", "b" ], [ "c", "d" ] ] }}
🌐
Skytowner
skytowner.com › explore › pandas_dataframe_to_json_method
Pandas DataFrame | to_json method with Examples
For situations like this when the conversion is improper, we can use the default_handler parameter to control what is returned: ... The handler takes as argument the source DataFrame and returns a serialisable object like a map, Series, DataFrame and so on. Now, instead of the malformed JSON that we had before, we can return another JSON of our liking. ... Here, the second line has 3 whitespaces, while the third has 6 whitespaces, and so on. ... Ask a question or leave a feedback... Official Pandas Documentation https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › DataFrame › to_json
Python Pandas DataFrame to_json() - Convert to JSON | Vultr Docs
December 31, 2024 - Converting a DataFrame to JSON involves calling the to_json() method, which serializes the DataFrame object to a JSON string.
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas documentation
orient='table' contains a ‘pandas_version’ field under ‘schema’. This stores the version of pandas used in the latest revision of the schema. ... >>> from json import loads, dumps >>> df = pd.DataFrame( ... [["a", "b"], ["c", "d"]], ... index=["row 1", "row 2"], ... columns=["col 1", "col 2"], ... ) >>> result = df.to_json(orient="split") >>> parsed = loads(result) >>> dumps(parsed, indent=4) {{ "columns": [ "col 1", "col 2" ], "index": [ "row 1", "row 2" ], "data": [ [ "a", "b" ], [ "c", "d" ] ] }}
🌐
GitHub
github.com › pandas-dev › pandas › issues › 35849
ENH: Enable `append mode` to JSON lines · Issue #35849 · pandas-dev/pandas
August 22, 2020 - Include the argument mode: str = "w"on to_json this will NOT break anything as the default behavior continues as write mode · Include a conditional in case mode="a", checking if orient='records' and lines=True, raising an Exception otherwise
Author   nullhack
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas – convert dataframe to json string
Pandas - Convert DataFrame to JSON String - Spark By {Examples}
November 5, 2024 - You can convert Pandas DataFrame to JSON string by using the DataFrame.to_json() method. This method takes a very important param orient which accepts values ‘columns‘, ‘records‘, ‘index‘, ‘split‘, ‘table‘, and ‘values‘. ...
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.24.2 › reference › api › pandas.DataFrame.to_json.html
pandas.DataFrame.to_json — pandas 0.24.2 documentation
>>> df = pd.DataFrame([['a', 'b'], ['c', 'd']], ... index=['row 1', 'row 2'], ... columns=['col 1', 'col 2']) >>> df.to_json(orient='split') '{"columns":["col 1","col 2"], "index":["row 1","row 2"], "data":[["a","b"],["c","d"]]}' Encoding/decoding a Dataframe using 'records' formatted JSON.
🌐
Educative
educative.io › answers › how-to-convert-data-to-a-json-format-using-pandas
How to convert data to a JSON format using pandas
Line 5–8: We convert a dictionary of three values into a pandas DataFrame. Line 10: We transform the DataFrame content into JSON format using df.to_json().
🌐
Prodigy
support.prodi.gy › t › convert-pandas-dataframe-to-suitable-jsonl-file › 3262
Convert pandas dataframe to suitable jsonl file - usage - Prodigy Support
August 4, 2020 - Hi all, I have a dataframe with a text colum, which I‘d like to annotate and other columns with metadata. As the strings in my text column have to start and to end with \n (I would like to annotate it literally). I have trouble to convert the dataframe to a suitable jsonl-file.