DataFrame.to_dict

DataFrame.to_dict(orient=’dict’, into=)
将DataFrame转换为字典。

Parameters

orient 确定字典值的类型:
- dict:dict like {column: {index: value}}
- list:dict like {column: [values]}
- series:dict like {column: Series(values)}
- split:dict like {"index": [index], "columns": [columns], "data": [values]}
- records:dict like [{column: value}, ..., {column: value}]
- index:dict like {index: {column: value}}
into 返回值中用于所有映射的collections.abc.Mapping子类。

Example:orient = “dict”

  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']}, index=['first', 'second', 'third', 'forth'])
  6. data.to_dict(orient="dict")
  1. {
  2. "site": {
  3. "first": "google",
  4. "second": "baidu",
  5. "third": "wiki",
  6. "forth": "pandas"
  7. },
  8. "age": {
  9. "first": 18,
  10. "second": 39,
  11. "third": 22,
  12. "forth": 45
  13. },
  14. "price": {
  15. "first": 1.0,
  16. "second": 2.0,
  17. "third": 3.0,
  18. "forth": 4.0
  19. },
  20. "color": {
  21. "first": "red",
  22. "second": "black",
  23. "third": "blue",
  24. "forth": "red"
  25. }
  26. }

Example:orient = “list”

  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', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])
  6. data.to_dict(orient="list")
  1. {
  2. "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", "blue", "red"]
  6. }

Example:orient = “series”

  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', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])
  6. data.to_dict(orient="series")
  1. {
  2. "site": first google
  3. second baidu
  4. third wiki
  5. forth pandas
  6. Name: site, dtype: object,
  7. "age": first 18
  8. second 39
  9. third 22
  10. forth 45
  11. Name: age, dtype: int64,
  12. "price": first 1.0
  13. second 2.0
  14. third 3.0
  15. forth 4.0
  16. Name: price, dtype: float64,
  17. "color": first red
  18. second black
  19. third blue
  20. forth red
  21. Name: color, dtype: object}

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', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])
  6. data.to_dict(orient="split")
  1. {
  2. "index": ["first", "second", "third", "forth"],
  3. "columns": ["site", "age", "price", "color"],
  4. "data": [
  5. ["google", 18, 1.0, "red"],
  6. ["baidu", 39, 2.0, "black"],
  7. ["wiki", 22, 3.0, "blue"],
  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', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])
  6. data.to_dict(orient="records")
  1. [{
  2. "site": "google",
  3. "age": 18,
  4. "price": 1.0,
  5. "color": "red"
  6. },
  7. {
  8. "site": "baidu",
  9. "age": 39,
  10. "price": 2.0,
  11. "color": "black"
  12. },
  13. {
  14. "site": "wiki",
  15. "age": 22,
  16. "price": 3.0,
  17. "color": "blue"
  18. },
  19. {
  20. "site": "pandas",
  21. "age": 45,
  22. "price": 4.0,
  23. "color": "red"
  24. }
  25. ]

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', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])
  6. data.to_dict(orient="index")
  1. {
  2. "first": {
  3. "site": "google",
  4. "age": 18,
  5. "price": 1.0,
  6. "color": "red"
  7. },
  8. "second": {
  9. "site": "baidu",
  10. "age": 39,
  11. "price": 2.0,
  12. "color": "black"
  13. },
  14. "third": {
  15. "site": "wiki",
  16. "age": 22,
  17. "price": 3.0,
  18. "color": "blue"
  19. },
  20. "forth": {
  21. "site": "pandas",
  22. "age": 45,
  23. "price": 4.0,
  24. "color": "red"
  25. }
  26. }