When working with data, it’s common to encounter JSON (JavaScript Object Notation) files, which are widely used for storing and exchanging data. Pandas, a powerful data manipulation library in Python, provides a convenient way to convert JSON data into a Pandas data frame. In this article, we’ll explore how to convert JSON data into a Pandas DataFrame, covering various scenarios and options you might encounter along the way.
What is a JSON file?JSON stands for Java script object notation. It stores the data in the format of a key-value pair . We can read the data from the various file formats such as CSV, JSON and Excel. The pandas library is used to work with the data frames and manipulate the data frames. we can read data from various files with the help of pandas .
- Importing the pandas Library.
- Reading the JSON file.
- Converting into data frame .
- Printing the data frame.
Pandas Convert JSON to DataFrameImporting the pandasThis is the first step to working with the data frames in Pandas. First, we import Panda’s library from Python. To convert a file to the data frame, we need to have a JSON file to perform that operation. First, we will create a JSON file or we will just download a Json file.
For importing the pandas library in python we need to use the import statement:
Python
Using json Module to create a fileHere, we will create a sample json file here the json file is as shown below .
Python
import json
data = {
"Name": {
"0": "Harsha",
"1": "Vardhan",
"2": "Krishna",
"3": "Hanuman",
"4": "Shiva"
},
"Roll_no": {
"0": 1,
"1": 2,
"2": 3,
"3": 4,
"4": 5
},
"subject": {
"0": "C",
"1": "JAVA",
"2": "C++",
"3": "SWIFT",
"4": "PYTHON"
}
}
with open('subject.json', 'w') as json_file:
json.dump(data, json_file, indent=4)
- In the above code we have created a json file and the json file consists of key value pair.
- The data is stored in the form of strings as a keys and values as a list .
- We will read the json file with the help of the read_json() to read the contents of the file .
Converting into DataFrame :
Python
#Importing the pandas Library
import pandas as pd
#Reading the JSON File
dataFrame = pd.read_json("subject.json")
#Printing the data Frame
print(dataFrame)
Output :
Name Roll_no subject 0 Harsha 1 C 1 Vardhan 2 JAVA 2 Krishna 3 C++ 3 Hanuman 4 SWIFT 4 Shiva 5 PYTHON Now, we will implement the same on a downloaded dataset,
Conclusion : As mentioned in the above way we can read the data from the JSON File from the Pandas and we can convert the JSON File into the DataFrame in the DataFrame .
Pandas Convert JSON to DataFrame – FAQsHow to Convert JSON Data to Python Data?To convert JSON data into Python data structures, you can use Python’s built-in json module. This conversion is straightforward, turning JSON into native Python dictionaries, lists, etc.:
import json
# Example JSON data as string json_data = '{"name": "John", "age": 30, "city": "New York"}'
# Convert JSON to Python python_data = json.loads(json_data) print(python_data) # Outputs: {'name': 'John', 'age': 30, 'city': 'New York'}
How to Read JSON into Pandas?To read JSON data into a pandas DataFrame, use the read_json() method. This is useful for handling JSON data directly from files or JSON strings:
import pandas as pd
# Assuming json_data is a JSON string df = pd.read_json(json_data) print(df) If you have a file containing JSON data, you can read it directly:
# Load JSON data from a file df = pd.read_json('path_to_file.json') print(df)
How to Convert DataFrame to JSON in Python?To convert a pandas DataFrame into a JSON format, use the to_json() method. This method is versatile and allows you to specify the format of the JSON output:
df = pd.DataFrame({ 'name': ['John', 'Jane'], 'age': [30, 25], 'city': ['New York', 'Los Angeles'] })
# Convert DataFrame to JSON json_output = df.to_json(orient='records') print(json_output)
How to Save a JSON File Using Pandas?To save a DataFrame as a JSON file using pandas, use the to_json() method with a file path:
# Save DataFrame to a JSON file df.to_json('output_file.json', orient='records')
How to Convert JSON to CSV in DataFrame?To convert JSON data to a CSV file using pandas, first read the JSON into a DataFrame and then use the to_csv() method to save it as CSV:
# Load JSON into DataFrame df = pd.read_json(json_data)
# Save DataFrame to CSV df.to_csv('output_file.csv', index=False)
|