Convert JSON to Pandas DataFrame
This article will show you how to convert JSON to Pandas DataFrame.
JSON is the abbreviation of JavaScript Object Notation. It is a format based on objects in JavaScript and is an encoding technology for representing structured data. It is a format based on objects in JavaScript and is an encoding technology for representing structured data. Currently, it is widely used, especially for sharing data between servers and web applications.
JSON is becoming very popular due to its simplicity and influence on programming language data structures. It is relatively easy to understand, below is an example of a JSON response from a simple API.
{
"Results":
[
{ "id": "01", "Name": "Jay" },
{ "id": "02", "Name": "Mark" },
{ "id": "03", "Name": "Jack" }
],
"status": ["ok"]
}
As you can see in our example, JSON seems to be a combination of nested lists and dictionaries to some extent, so it is relatively easy to extract data from the JSON file and even store it as a Pandas DataFrame.
Pandas and JSON libraries in Python can help with this. We have two functions read_json()
and json_normalize()
that can help convert a JSON string into a DataFrame.
json_normalize()
Convert JSON to Pandas DataFrame using
json_normalize()
The function is very widely used to read a nested JSON string and return a DataFrame. To use this function, we first need to json.loads()
read the JSON string using the function from the JSON library in Python, and then we pass this JSON object to json_normalize()
, which will return a Pandas DataFrame containing the required data.
import pandas as pd
import json
from pandas import json_normalize
data = """
{
"Results":
[
{ "id": "1", "Name": "Jay" },
{ "id": "2", "Name": "Mark" },
{ "id": "3", "Name": "Jack" }
],
"status": ["ok"]
}
"""
info = json.loads(data)
df = json_normalize(info["Results"]) # Results contain the required data
print(df)
Output:
id Name
0 1 Jay
1 2 Mark
2 3 Jack
read_json()
Convert JSON to Pandas DataFrame using
Another Pandas function to convert JSON to DataFrame is read_json()
for simple JSON string. We can directly pass the path of JSON file or JSON string to the function to store the data in Pandas DataFrame. read_json()
There are many parameters among which orient
specifies the format of JSON string.
The downside is that it is difficult to use with nested JSON strings. So for the sake of usage read_json()
, we will use a much simpler example as shown below.
import pandas as pd
data = """
{
"0":{
"Name": "Jay",
"Age": "17"
},
"1":{
"Name": "Mark",
"Age": "15"
},
"2":{
"Name": "Jack",
"Age":"16"
}
}
"""
df = pd.read_json(data, orient="index")
print(df)
Output:
Name Age
0 Jay 17
1 Mark 15
2 Jack 16
We orient
set to 'index'
because the JSON string fromat matches the pattern {index : {column: value}}
.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to Convert DataFrame Column to String in Pandas
Publish Date:2025/05/02 Views:161 Category:Python
-
We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a
How to count the frequency of values in a Pandas DataFrame
Publish Date:2025/05/02 Views:84 Category:Python
-
Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().
How to get value from Pandas DataFrame cell
Publish Date:2025/05/02 Views:147 Category:Python
-
We'll look at using to get values from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .
How to Add a Row to a Pandas DataFrame
Publish Date:2025/05/02 Views:127 Category:Python
-
Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to
How to change the order of Panas DataFrame columns
Publish Date:2025/05/02 Views:184 Category:Python
-
We will show how to use insert and reindex to change the order of columns in different ways pandas.DataFrame , such as assigning column names in a desired order. pandas.DataFrame Sort the columns in the new order The easiest way is columns
How to pretty print an entire Pandas Series/DataFrame
Publish Date:2025/05/02 Views:167 Category:Python
-
We will introduce various methods to pretty print the entire Pandas Series/DataFrame, such as option_context, set_option, and options.display. option_context Pretty Printing Pandas DataFrame We can option_context use with one or more option
How to count the number of NaN occurrences in a Pandas Dataframe column
Publish Date:2025/05/02 Views:144 Category:Python
-
We will look at methods for counting the number of NaN occurrences in a column of a Pandas DataFrame. We have a number of options, including isna() the method for one or more columns, by NaN subtracting the total length from the number of o
How to Convert a Pandas Dataframe to a NumPy Array
Publish Date:2025/05/02 Views:151 Category:Python
-
We will introduce to_numpy() the method to pandas.Dataframe convert a to NumPy an array, which is introduced in pandas v0.24.0, replacing the old .values method. We can define it on Index , Series , and DataFrame objects to_numpy . The old
How to add a header row to a Pandas DataFrame
Publish Date:2025/05/02 Views:161 Category:Python
-
We will look at methods for adding a header row to a pandas dataframe, as well as the option to pass in the names directly in the dataframe or by assigning the column names in a list directly to dataframe.columns the method. We will also in