Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Python Pandas read_json() Method



Theread_json() method in Python's Pandas library allows you to read or load data from a JSON file or JSON string into a Pandas object. This method supports multiple configurations, including reading nested JSON structures, parsing dates, managing missing values, and selecting specific data. This method is widely used in data analysis as it easily handles the structured data.

JSON (JavaScript Object Notation) is a popular lightweight data format for exchanging and storing structured information. It uses plain-text formatting, where each element is represented in a hierarchical structure, making it easy to read and process. JSON files have the.json extension and are commonly used in web applications, APIs, and data pipelines.

Syntax

The syntax of the Pandas read_json() method is as follows −

pandas.read_json(path_or_buf, *, orient=None, typ='frame', dtype=None, convert_axes=None, convert_dates=True, keep_default_dates=True, precise_float=False, date_unit=None, encoding=None, encoding_errors='strict', lines=False, chunksize=None, compression='infer', nrows=None, storage_options=None, dtype_backend=<no_default>, engine='ujson')

Parameters

The Python Pandas read_json() method accepts the below parameters −

  • path_or_buf: A string representing the file path, URL, or file-like object to read JSON data from. Supports various schemes like http, ftp, s3, etc.

  • orient: Specifies the expected format of JSON string. Common values include "split", "records", "index", "columns", and "values".

  • typ: Defines the type of object to return (frame for DataFrame, series for Series).

  • dtype: Defines the data type of columns. If set toTrue, pandas automatically detects and assign data types for each column. If set toFalse, Pandas do not infer data types, keep the original data as it is. You can also use a dictionary for mapping column names to their data types. By default, it is set toNone.

  • convert_axes: Converts axes to the specified data type. Default isNone.

  • convert_dates: IfTrue, attempts to parse date-like strings. If set toFalse, no dates will be converted. If you give a list of column names, then those columns will be converted and default datelike columns may also be converted (depending on keep_default_dates parameter).

  • keep_default_dates: If date parsing is enabled (convert_dates is not set to False), the method will attempt to automatically identify and parse columns with date-like names.

  • precise_float: Enables the use of a higher precision (strtod) function for decoding string to double values.

  • date_unit: Specifies the timestamp unit to detect when converting dates. Automatically detects the appropriate precision unless explicitly set.

  • encoding: Specifies the encoding to decode Python 3 byte objects.

  • encoding_errors: Defines how encoding errors are handled. Possible values are "strict", "ignore", "replace"

  • lines: Indicates whether the file should be read as one JSON object per line.

  • chunksize: Reads the file in chunks of the specified size (useful for large files).

  • compression: Specifies the compression type (e.g., gzip, bz2, zip, etc.). Default is infer.

  • storage_options: Additional options for connecting to certain storage back-ends (e.g., AWS S3, Google Cloud Storage).

  • dtype_backend: Determines the backend data type for the resultant DataFrame. This feature is experimental.

  • engine: Specifies the parser engine to use for reading JSON. Default engine is "ujson". The "pyarrow" engine is only available whenlines=True.

Return Value

The Pandasread_json() method returns a Series, DataFrame, orpandas.api.typing.JsonReader object, depending on whetherchunksize is set. AJsonReader is returned when thechunksize is not 0 or None. Otherwise, the return type depends on the value specified in thetyp parameter.

Example: Basic Example of Reading JSON String

Basic usage of theread_json() method to read a JSON string into a DataFrame. Here we will use theStringIO to represent the JSON string as an in-memory buffer

import pandas as pdfrom io import StringIOjson_data = '{"col1": {"row1": "a", "row2": "b"}, "col2": {"row1": "c", "row2": "d"}}'# Read the JSON stringdf = pd.read_json(StringIO(json_data))print("DataFrame from JSON String:")print(df)

The output of the above code is as follows −

DataFrame from JSON String:
col1col2
row1ac
row2bd

Example: Reading a Simple JSON File

Here is a basic example demonstrating reading a simple JSON file using the pandasread_json() method.

import pandas as pd# Reading a JSON filedf = pd.read_json('example_json_file.json')print("DataFrame from JSON File:")print(df)

When we run above program, it produces following result −

DataFrame from JSON File:
CarDate_of_purchase
0BMW10-10-2024
1Lexus12-10-2024
2Audi17-10-2024
3Mercedes16-10-2024
4Jaguar19-10-2024
5Bentley22-10-2024

Example: Reading JSON Data from an URL

This example reads the JSON data from an URL using the pandasread_json() method.

import pandas as pdurl ="https://raw.githubusercontent.com/domoritz/maps/refs/heads/master/data/iris.json"# Read JSON into a Pandas DataFrame df = pd.read_json(url)print("DataFrame from JSON file using an URL:")print(df.head(5))

Following is an The output of the above code is as follows −

DataFrame from JSON file using an URL:
sepalLengthsepalWidthpetalLengthpetalWidthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa

Example: Reading JSON with 'records' Orientation

This example demonstrates reading JSON data with 'records' orientation using theorient parameter of the pandasread_json() method.

import pandas as pdfrom io import StringIO# Sample JSONdata = """[    {"Name": "Kiran", "Gender": "Male", "Age": 30},    {"Name": "Hema", "Gender": "Female", "Age": 25},    {"Name": "priya", "Gender": "Female", "Age": 35}]"""# Read JSONdf = pd.read_json(StringIO(data), orient='records')print(df)

While executing the above code we get the following output −

NameGenderAge
0KiranMale30
1HemaFemale25
2priyaFemale35

Example: Reading Compressed JSON Data into a DataFrame

Theread_json() method allows you to read the data from a compressed JSON file using thecompression parameter of theread_json() method.

import pandas as pd# Create a DataFramedf = pd.DataFrame([    {"Name": "Kiran", "Gender": "Male", "Age": 30},    {"Name": "Hema", "Gender": "Female", "Age": 25},    {"Name": "priya", "Gender": "Female", "Age": 35}])# Convert to JSON fileresult = df.to_json('dataframe_to_json_data.json.gz')# Read compressed JSONdf = pd.read_json('dataframe_to_json_data.json.gz', compression='gzip')print("DataFrame from compressed JSON file:")print(df)

Following is an The output of the above code is as follows −

DataFrame from compressed JSON file:
NameGenderAge
NameKiranMale30
GenderHemaFemale25
AgepriyaFemale35
python_pandas_io_tool.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp