pandas.Series.to_json#

Series.to_json(path_or_buf=None, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit='ms', default_handler=None, lines=False, compression='infer', index=True, indent=None, storage_options=None)[源代码]#

将对象转换为JSON字符串。

注意NaN‘s和None将被转换为NULL,而DateTime对象将被转换为UNIX时间戳。

参数
path_or_buf字符串、路径对象、类似文件的对象或无,默认为无

字符串、路径对象(实现os.PathLike [str] )或实现WRITE()函数的类似文件的对象。如果没有,则以字符串形式返回结果。

orient应力

预期JSON字符串格式的指示。

  • 系列:

    • 默认设置为‘index’

    • 允许的值为:{‘拆分’,‘记录’,‘索引’,‘表’}。

  • DataFrame:

    • 缺省值为‘Columns’

    • 允许的值为:{‘拆分’,‘记录’,‘索引’,‘列’,‘值’,‘表’}。

  • JSON字符串的格式:

    • ‘Split’:Dict Like{‘index’-> [索引] ,‘列’-> [列] ,‘数据’-> [值] }

    • 《唱片》:列表如下 [{{column -> value}}, ... , {{column -> value}}]

    • ‘index’:字典Like{index->{Column->Value}}

    • ‘Columns’:Dict Like{Column->{index->Value}}

    • ‘VALUES’:仅值数组

    • ‘TABLE’:字典LIKE{‘SCHEMA’:{SCHEMA},‘DATA’:{DATA}}

    描述数据,其中数据组件类似于 orient='records'

date_format{无,‘纪元’,‘iso’}

日期转换的类型。‘pech’=纪元毫秒,‘iso’=ISO8601。默认设置取决于 orient 。为 orient='table' ,默认为‘iso’。对于所有其他方向,缺省值为‘EPOCH’。

double_precision整型,默认为10

对浮点值进行编码时要使用的小数位数。

force_ascii布尔值,默认为True

强制将编码字符串设置为ASCII。

date_unit字符串,默认‘ms’(毫秒)

要编码的时间单位控制时间戳和ISO8601精度。分别表示秒、毫秒、微秒和纳秒的“s”、“ms”、“us”和“ns”之一。

default_handler可调用,默认为无

如果对象无法以其他方式转换为适合JSON的格式,则调用的处理程序。应接收单个参数,该参数是要转换并返回可序列化对象的对象。

lines布尔值,默认为False

如果‘Orient’是‘Records’,则写出以行分隔的json格式。如果不正确的‘Orient’,将抛出ValueError,因为其他对象不是类似列表的。

compression字符串或词典,默认为‘INFER’

For on-the-fly compression of the output data. If 'infer' and 'path_or_buf' path-like, then detect compression from the following extensions: '.gz', '.bz2', '.zip', '.xz', or '.zst' (otherwise no compression). Set to None for no compression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, or zstandard.ZstdDecompressor, respectively. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}.

在 1.4.0 版更改: Z标准支持。

index布尔值,默认为True

是否将索引值包括在JSON字符串中。不包括索引 (index=False )仅当Orient为‘Split’或‘table’时才受支持。

indent整型,可选

用于缩进每条记录的空格长度。

1.0.0 新版功能.

storage_optionsDICT,可选

对特定存储连接有意义的额外选项,例如主机、端口、用户名、密码等。对于HTTP(S)URL,键-值对被转发到 urllib.request.Request 作为标题选项。对于其他URL(例如,以“s3://”和“gcs://”开头),键-值对被转发到 fsspec.open 。请看 fsspecurllib 有关更多详细信息和有关存储选项的更多示例,请参阅 here

1.2.0 新版功能.

退货
无或字符串

如果PATH_OR_BUF为NONE,则以字符串形式返回结果json格式。否则返回None。

参见

read_json

将JSON字符串转换为Pandas对象。

注意事项

的行为 indent=0 与stdlib不同,stdlib不缩进输出但插入换行符。目前, indent=0 和默认设置 indent=None 在Pandas身上也是一样的,尽管这一点可能会在未来的版本中发生变化。

orient='table' 在‘SCHEMA’下包含‘PANDAS_VERSION’字段。这存储了的版本 pandas 在架构的最新修订版本中使用。

示例

>>> import json
>>> df = pd.DataFrame(
...     [["a", "b"], ["c", "d"]],
...     index=["row 1", "row 2"],
...     columns=["col 1", "col 2"],
... )
>>> result = df.to_json(orient="split")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)  
{
    "columns": [
        "col 1",
        "col 2"
    ],
    "index": [
        "row 1",
        "row 2"
    ],
    "data": [
        [
            "a",
            "b"
        ],
        [
            "c",
            "d"
        ]
    ]
}

使用对数据帧进行编码/解码 'records' 格式化的JSON。请注意,此编码不会保留索引标签。

>>> result = df.to_json(orient="records")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)  
[
    {
        "col 1": "a",
        "col 2": "b"
    },
    {
        "col 1": "c",
        "col 2": "d"
    }
]

使用对数据帧进行编码/解码 'index' 格式化的JSON:

>>> result = df.to_json(orient="index")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)  
{
    "row 1": {
        "col 1": "a",
        "col 2": "b"
    },
    "row 2": {
        "col 1": "c",
        "col 2": "d"
    }
}

使用对数据帧进行编码/解码 'columns' 格式化的JSON:

>>> result = df.to_json(orient="columns")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)  
{
    "col 1": {
        "row 1": "a",
        "row 2": "c"
    },
    "col 2": {
        "row 1": "b",
        "row 2": "d"
    }
}

使用对数据帧进行编码/解码 'values' 格式化的JSON:

>>> result = df.to_json(orient="values")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)  
[
    [
        "a",
        "b"
    ],
    [
        "c",
        "d"
    ]
]

使用表架构进行编码:

>>> result = df.to_json(orient="table")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)  
{
    "schema": {
        "fields": [
            {
                "name": "index",
                "type": "string"
            },
            {
                "name": "col 1",
                "type": "string"
            },
            {
                "name": "col 2",
                "type": "string"
            }
        ],
        "primaryKey": [
            "index"
        ],
        "pandas_version": "1.4.0"
    },
    "data": [
        {
            "index": "row 1",
            "col 1": "a",
            "col 2": "b"
        },
        {
            "index": "row 2",
            "col 1": "c",
            "col 2": "d"
        }
    ]
}