Loading JSON Files in Pandas
This tutorial shows how to pandas.read_json()
load a JSON file into a Pandas DataFrame using the method.
Loading JSON File into Pandas DataFrame
We can load the JSON file into a Pandas DataFrame using pandas.read_json()
the load_data function by passing the path of the JSON file as a parameter to the load_data function.pandas.read_json()
{
"Name": {"1": "Anil", "2": "Biraj", "3": "Apil", "4": "Kapil"},
"Age": {"1": 23, "2": 25, "3": 28, "4": 30},
}
The content of the example data.json
file is shown above. We will create a DataFrame from the above JSON file.
import pandas as pd
df = pd.read_json("data.json")
print("DataFrame generated using JSON file:")
print(df)
Output:
DataFrame generated using JSON file:
Name Age
1 Anil 23
2 Biraj 25
3 Apil 28
4 Kapil 30
It shows data.json
the DataFrame generated from the data in the file. We must ensure that the file is available in the current working directory data.json
to generate the DataFrame, otherwise we need to provide the full path to the JSON file as pandas.read_json()
a parameter to the method.
The DataFrame formed from a JSON file depends on the orientation of the JSON file. We generally have three different orientations of JSON files.
- Index-oriented
- Value-oriented
- Column-oriented
Loading an index-oriented JSON file into a Pandas DataFrame
{
"0": {"Name": "Anil", "Age": 23},
"1": {"Name": "Biraj", "Age": 25},
"2": {"Name": "Apil", "Age": 26},
}
This is an example of an index-oriented JSON file, where the top-level keys represent the index of the data.
import pandas as pd
df = pd.read_json("data.json")
print("DataFrame generated from Index Oriented JSON file:")
print(df)
Output:
DataFrame generated from Index Oriented JSON file:
0 1 2
Name Anil Biraj Apil
Age 23 25 26
It will data.json
create a DataFrame from the file, with the top-level keys represented as columns in the DataFrame.
Loading a value-oriented JSON file into a Pandas DataFrame
[["Anil", 23], ["Biraj", 25], ["Apil", 26]]
This is an example of a value-oriented JSON file, where each element in the array represents the value of each row.
import pandas as pd
df = pd.read_json("data.json")
print("DataFrame generated from Value Oriented JSON file:")
print(df)
Output:
DataFrame generated from Value Oriented JSON file:
0 1
0 Anil 23
1 Biraj 25
2 Apil 26
It will data.json
create a DataFrame from the file, and each element of the array in the JSON file will be represented as a row in the DataFrame.
Loading a column-oriented JSON file into a Pandas DataFrame
{"Name": {"1": "Anil", "2": "Biraj", "3": "Apil"}, "Age": {"1": 23, "2": 25, "3": 28}}
It is an example of a top-level index for a column-oriented JSON file, representing the column names of the data.
import pandas as pd
df = pd.read_json("data.json")
print("DataFrame generated from Column Oriented JSON file:")
print(df)
Output:
DataFrame generated from Column Oriented JSON file:
Name Age
1 Anil 23
2 Biraj 25
3 Apil 28
It will data.json
create a DataFrame from the file, with the top-level index of the JSON file as the column names in the DataFrame.
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