How to Convert a DataFrame to JSON Array in Python | DataFrame to JSON List [4 Methods]

In this Pandas article, I will tell you “How to convert a dataframe to json array in Python” or “How to convert a dataframe to json list” using different methods and multiple examples.

Pandas dataframe is a two-dimensional labeled data structure with columns of potentially different types in Python.

JSON is a lightweight data-interchange format that is easy for us to read and write.

Python provides the to_json() method to convert a dataframe to json list in Python. But we can also use other methods, such as the to_dict() method, list comprehension, and apply() function with a lambda function.

Let’s dive into the methods and learn how to use them to convert a Pandas dataframe to json array in Python.

1. Convert a DataFrame to JSON Array in Python using the to_json() method

Pandas provides the to_json() method to convert DataFrame to a JSON string. However, by default, this method returns a JSON object, not a list.

We can overcome this by specifying the orient parameter.

Here is an example:

import pandas as pd

Sales_Persons = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [30, 35, 40],
        'City': ['New York', 'Los Angeles', 'Chicago']}
Sales_df = pd.DataFrame(Sales_Persons)
json_list = Sales_df.to_json(orient='records')
print(json_list)

Output:

[{"Name":"Alice","Age":30,"City":"New York"},{"Name":"Bob","Age":35,"City":"Los Angeles"},{"Name":"Charlie","Age":40,"City":"Chicago"}]

The screenshot below features the output after executing the Pycharm editor’s code.

How to Convert a DataFrame to JSON Array in Python

2. Pandas dataframe to json array using the to_dict() method

In this process, first, we have to convert the DataFrame to a dictionary in Python using the to_dict() method and then convert the dictionary to a JSON list.

Below is the full code to convert a dataframe to json in Python using the to_dict() method:

import pandas as pd
import json

Employees = {'Name': ['Sam', 'Monica', 'Charlie'],
        'Age': [30, 35, 40],
        'Department': ['Technology', 'Marketing', 'Manager']}
Employee_df = pd.DataFrame(Employees)
dict_data = Employee_df.to_dict(orient='records')
json_list = json.dumps(dict_data)
print(json_list)

Here, we use to_dict(orient=’records’) to convert the DataFrame to a list of dictionaries in Python, where each dictionary represents a row.

Then, we will use the json.dumps() to convert the dictionary to a JSON string in Python.

dict_data = Employee_df.to_dict(orient='records')
json_list = json.dumps(dict_data)

Output:

[{"Name": "Sam", "Age": 30, "Department": "Technology"}, {"Name": "Monica", "Age": 35, "Department": "Marketing"}, {"Name": "Charlie", "Age": 40, "Department": "Manager"}]

The following screenshot illustrates the result after executing the code in the Pycharm editor.

pandas to json array in Python

3. Python dataframe to json array using list comprehension

The list comprehension method is used to iterate over the DataFrame rows and convert each row to a dictionary in Python, then collect these dictionaries into a list.

Finally, we can use the json.dumps() function to convert the list of dictionaries to a JSON string.

Here is an instance:

import pandas as pd
import json

Products = {'Name': ['Phone', 'Laptop', 'Headphones'], 'Companies': ['Apple', 'hp', 'Samsung']}
Products_df = pd.DataFrame(Products)
json_list = [row.to_dict() for _, row in Products_df.iterrows()]
json_data = json.dumps(json_list)
print(json_data)

Output:

[{"Name": "Phone", "Companies": "Apple"}, {"Name": "Laptop", "Companies": "hp"}, {"Name": "Headphones", "Companies": "Samsung"}]

The screenshot below presents the output obtained after the code execution in the Pycharm editor.

convert dataframe to json in python

4. Convert dataframe to json using the apply() function

We can use the apply() function and a lambda function in Python to convert each row of the DataFrame to a dictionary. Then, we can collect these dictionaries into a list and convert it to a JSON string using json.dumps() function.

Here is an instance to do so:

import pandas as pd
import json

nested_data = {'Name': ['Alice', 'Bob'],
               'Info': [{'Age': 30, 'City': 'New York'}, {'Age': 35, 'City': 'Los Angeles'}]}
df = pd.DataFrame(nested_data)
json_list = df.apply(lambda row: row.to_dict(), axis=1).tolist()
json_data = json.dumps(json_list)
print(json_data)

Output:

[{"Name": "Alice", "Info": {"Age": 30, "City": "New York"}}, {"Name": "Bob", "Info": {"Age": 35, "City": "Los Angeles"}}]

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

pandas dataframe to json without index in Python

Conclusion

This tutorial is about how to convert a dataframe to json array in Python or convert a dataframe to json list using four different methods like the to_json() function, to_dict() method, list comprehension, and apply() function with a lambda function with some illustrative examples.

Now the choice is yours to use the methods according to your requirements.

You may also like to read the following articles related to Pandas: