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”
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']}, index=['first', 'second', 'third', 'forth'])data.to_dict(orient="dict")
{"site": {"first": "google","second": "baidu","third": "wiki","forth": "pandas"},"age": {"first": 18,"second": 39,"third": 22,"forth": 45},"price": {"first": 1.0,"second": 2.0,"third": 3.0,"forth": 4.0},"color": {"first": "red","second": "black","third": "blue","forth": "red"}}
Example:orient = “list”
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', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])data.to_dict(orient="list")
{"site": ["google", "baidu", "wiki", "pandas"],"age": [18, 39, 22, 45],"price": [1.0, 2.0, 3.0, 4.0],"color": ["red", "black", "blue", "red"]}
Example:orient = “series”
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', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])data.to_dict(orient="series")
{"site": first googlesecond baiduthird wikiforth pandasName: site, dtype: object,"age": first 18second 39third 22forth 45Name: age, dtype: int64,"price": first 1.0second 2.0third 3.0forth 4.0Name: price, dtype: float64,"color": first redsecond blackthird blueforth redName: color, dtype: object}
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', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])data.to_dict(orient="split")
{"index": ["first", "second", "third", "forth"],"columns": ["site", "age", "price", "color"],"data": [["google", 18, 1.0, "red"],["baidu", 39, 2.0, "black"],["wiki", 22, 3.0, "blue"],["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', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])data.to_dict(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": "blue"},{"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', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])data.to_dict(orient="index")
{"first": {"site": "google","age": 18,"price": 1.0,"color": "red"},"second": {"site": "baidu","age": 39,"price": 2.0,"color": "black"},"third": {"site": "wiki","age": 22,"price": 3.0,"color": "blue"},"forth": {"site": "pandas","age": 45,"price": 4.0,"color": "red"}}
