DataFrame.to_json
DataFrame.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和None将被转换为null,datetime对象将被转换为UNIX时间戳。
| path_or_buf | 文件路径或对象。如果未指定,则返回字符串。 |
|---|---|
| orient | 指示预期的字符串格式: - split: dict like {"index": [index], "column": [columns], "data": [values]}- records: dict like [{column: value}, ..., {column: value}]- index: dict like {index: {column: value}}- columns: dict like {column: {index: value}}- values: dict like [[values]]- table: dict like {"sechma": {schema}, "data":{data}} |
| date_format | 日期转换类型。 epoch:毫秒;iso:ISO8601。 对于orient=table,默认为iso。其他默认为epoch |
| double_precision | 编码浮点值时使用的小数位数 |
| force_ascii | 强制编码字符串为ASCII |
| date_unit | 要编码的时间单位精度:s、ms、us、ns |
| default_handler | 如果对象无法以其他方式转换为适用于JSON的格式时,要调用的处理程序。 |
| lines | 如果orient=records,则标识是否写出以行分隔的JSON格式 |
| compression | 仅在第一个参数是文件时适用。表示在输出文件中使用的压缩方式:infer、gzip、bz2、zip、xz、None |
| index | 仅适用于orient=split或table时。标识是否在JSON字符串中包含索引值。 |
| indent | 用于缩进每条记录的空格长度 |
| storage_options |
Example:orient = ‘split’
import pandas as pddata = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],'age':[18, 39, 22, 45],'price': [1.0, 2.0, 3.0, 4.0],'color': ['red', 'black', None, 'red']})data.to_json(orient='split')
{"columns": ["site", "age", "price", "color"],"index": [0, 1, 2, 3],"data": [["google", 18, 1.0, "red"],["baidu", 39, 2.0, "black"],["wiki", 22, 3.0, null],["pandas", 45, 4.0, "red"]]}
Example:orient = ‘records’
import pandas as pddata = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],'age':[18, 39, 22, 45],'price': [1.0, 2.0, 3.0, 4.0],'color': ['red', 'black', None, 'red']})data.to_json(orient='records')
[{"site": "google","age": 18,"price": 1.0,"color": "red"}, {"site": "baidu","age": 39,"price": 2.0,"color": "black"}, {"site": "wiki","age": 22,"price": 3.0,"color": null}, {"site": "pandas","age": 45,"price": 4.0,"color": "red"}]
Example:orient = ‘index’
import pandas as pddata = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],'age':[18, 39, 22, 45],'price': [1.0, 2.0, 3.0, 4.0],'color': ['red', 'black', None, 'red']})data.to_json(orient='index')
{"0": {"site": "google","age": 18,"price": 1.0,"color": "red"},"1": {"site": "baidu","age": 39,"price": 2.0,"color": "black"},"2": {"site": "wiki","age": 22,"price": 3.0,"color": null},"3": {"site": "pandas","age": 45,"price": 4.0,"color": "red"}}
Example:orient = ‘columns’
import pandas as pddata = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],'age':[18, 39, 22, 45],'price': [1.0, 2.0, 3.0, 4.0],'color': ['red', 'black', None, 'red']})data.to_json(orient='columns')
{"site": {"0": "google","1": "baidu","2": "wiki","3": "pandas"},"age": {"0": 18,"1": 39,"2": 22,"3": 45},"price": {"0": 1.0,"1": 2.0,"2": 3.0,"3": 4.0},"color": {"0": "red","1": "black","2": null,"3": "red"}}
Example:orient = ‘values’
import pandas as pddata = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],'age':[18, 39, 22, 45],'price': [1.0, 2.0, 3.0, 4.0],'color': ['red', 'black', None, 'red']})data.to_json(orient='values')
[["google", 18, 1.0, "red"],["baidu", 39, 2.0, "black"],["wiki", 22, 3.0, null],["pandas", 45, 4.0, "red"]]
Example:orient = ‘table’
import pandas as pddata = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],'age':[18, 39, 22, 45],'price': [1.0, 2.0, 3.0, 4.0],'color': ['red', 'black', None, 'red']})data.to_json(orient='table')
{"schema": {"fields": [{"name": "index","type": "integer"}, {"name": "site","type": "string"}, {"name": "age","type": "integer"}, {"name": "price","type": "number"}, {"name": "color","type": "string"}],"primaryKey": ["index"],"pandas_version": "0.20.0"},"data": [{"index": 0,"site": "google","age": 18,"price": 1.0,"color": "red"}, {"index": 1,"site": "baidu","age": 39,"price": 2.0,"color": "black"}, {"index": 2,"site": "wiki","age": 22,"price": 3.0,"color": null}, {"index": 3,"site": "pandas","age": 45,"price": 4.0,"color": "red"}]}
