Loading JSON Data into a pandas DataFrame with Python
In this post, we will walk through the process of loading JSON data into a pandas DataFrame using Python. JSON (JavaScript Object Notation) is a popular data format for exchanging data between a server and a web application. Pandas, a powerful data manipulation library in Python, makes it easy to work with JSON data.
Step-by-Step Guide
Sample JSON Data
Let’s assume you have JSON data stored in a file called data.json. Here’s an example of what the content might look like:
[
{
"name": "John",
"age": 30,
"city": "New York"
},
{
"name": "Anna",
"age": 22,
"city": "London"
},
{
"name": "Mike",
"age": 32,
"city": "San Francisco"
}
]
Loading JSON Data into a DataFrame
To load this JSON data into a pandas DataFrame, follow these steps:
- Read the JSON file:Use the
pd.read_json()function to read the JSON file and load it into a DataFrame. Here’s how you can do it:
# Read the JSON file
df = pd.read_json('data.json')
# Display the DataFrame
print(df)
- This will load the JSON data into a DataFrame and print it.
Example Code
Here’s the complete example code to load the JSON data into a DataFrame and display it:
import pandas as pd
# Read the JSON file
df = pd.read_json('data.json')
# Display the DataFrame
print(df)
Output
The output will be a DataFrame with the JSON data:
name age city
0 John 30 New York
1 Anna 22 London
2 Mike 32 San Francisco
Conclusion
Loading JSON data into a pandas DataFrame is straightforward with the pd.read_json() function. This allows you to leverage the powerful data manipulation capabilities of pandas to analyze and process your JSON data efficiently.