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’

  1. import pandas as pd
  2. data = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', None, 'red']})
  6. data.to_json(orient='split')
  1. {
  2. "columns": ["site", "age", "price", "color"],
  3. "index": [0, 1, 2, 3],
  4. "data": [
  5. ["google", 18, 1.0, "red"],
  6. ["baidu", 39, 2.0, "black"],
  7. ["wiki", 22, 3.0, null],
  8. ["pandas", 45, 4.0, "red"]
  9. ]
  10. }

Example:orient = ‘records’

  1. import pandas as pd
  2. data = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', None, 'red']})
  6. data.to_json(orient='records')
  1. [{
  2. "site": "google",
  3. "age": 18,
  4. "price": 1.0,
  5. "color": "red"
  6. }, {
  7. "site": "baidu",
  8. "age": 39,
  9. "price": 2.0,
  10. "color": "black"
  11. }, {
  12. "site": "wiki",
  13. "age": 22,
  14. "price": 3.0,
  15. "color": null
  16. }, {
  17. "site": "pandas",
  18. "age": 45,
  19. "price": 4.0,
  20. "color": "red"
  21. }]

Example:orient = ‘index’

  1. import pandas as pd
  2. data = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', None, 'red']})
  6. data.to_json(orient='index')
  1. {
  2. "0": {
  3. "site": "google",
  4. "age": 18,
  5. "price": 1.0,
  6. "color": "red"
  7. },
  8. "1": {
  9. "site": "baidu",
  10. "age": 39,
  11. "price": 2.0,
  12. "color": "black"
  13. },
  14. "2": {
  15. "site": "wiki",
  16. "age": 22,
  17. "price": 3.0,
  18. "color": null
  19. },
  20. "3": {
  21. "site": "pandas",
  22. "age": 45,
  23. "price": 4.0,
  24. "color": "red"
  25. }
  26. }

Example:orient = ‘columns’

  1. import pandas as pd
  2. data = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', None, 'red']})
  6. data.to_json(orient='columns')
  1. {
  2. "site": {
  3. "0": "google",
  4. "1": "baidu",
  5. "2": "wiki",
  6. "3": "pandas"
  7. },
  8. "age": {
  9. "0": 18,
  10. "1": 39,
  11. "2": 22,
  12. "3": 45
  13. },
  14. "price": {
  15. "0": 1.0,
  16. "1": 2.0,
  17. "2": 3.0,
  18. "3": 4.0
  19. },
  20. "color": {
  21. "0": "red",
  22. "1": "black",
  23. "2": null,
  24. "3": "red"
  25. }
  26. }

Example:orient = ‘values’

  1. import pandas as pd
  2. data = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', None, 'red']})
  6. data.to_json(orient='values')
  1. [
  2. ["google", 18, 1.0, "red"],
  3. ["baidu", 39, 2.0, "black"],
  4. ["wiki", 22, 3.0, null],
  5. ["pandas", 45, 4.0, "red"]
  6. ]

Example:orient = ‘table’

  1. import pandas as pd
  2. data = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', None, 'red']})
  6. data.to_json(orient='table')
  1. {
  2. "schema": {
  3. "fields": [{
  4. "name": "index",
  5. "type": "integer"
  6. }, {
  7. "name": "site",
  8. "type": "string"
  9. }, {
  10. "name": "age",
  11. "type": "integer"
  12. }, {
  13. "name": "price",
  14. "type": "number"
  15. }, {
  16. "name": "color",
  17. "type": "string"
  18. }],
  19. "primaryKey": ["index"],
  20. "pandas_version": "0.20.0"
  21. },
  22. "data": [{
  23. "index": 0,
  24. "site": "google",
  25. "age": 18,
  26. "price": 1.0,
  27. "color": "red"
  28. }, {
  29. "index": 1,
  30. "site": "baidu",
  31. "age": 39,
  32. "price": 2.0,
  33. "color": "black"
  34. }, {
  35. "index": 2,
  36. "site": "wiki",
  37. "age": 22,
  38. "price": 3.0,
  39. "color": null
  40. }, {
  41. "index": 3,
  42. "site": "pandas",
  43. "age": 45,
  44. "price": 4.0,
  45. "color": "red"
  46. }]
  47. }