@@ -991,21 +991,25 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None):
991
991
return cls(data, index=index, columns=columns, dtype=dtype)
992
992
993
993
def to_dict(self, orient='dict', into=dict):
994
- """Convert DataFrame to dictionary.
994
+ """
995
+ Convert the DataFrame to a dictionary.
996
+
997
+ The type of the key-value pairs can be customized with the parameters
998
+ (see below).
995
999
996
1000
Parameters
997
1001
----------
998
1002
orient : str {'dict', 'list', 'series', 'split', 'records', 'index'}
999
1003
Determines the type of the values of the dictionary.
1000
1004
1001
- - dict (default) : dict like {column -> {index -> value}}
1002
- - list : dict like {column -> [values]}
1003
- - series : dict like {column -> Series(values)}
1004
- - split : dict like
1005
- {index -> [index], columns -> [columns], data -> [values]}
1006
- - records : list like
1005
+ - ' dict' (default) : dict like {column -> {index -> value}}
1006
+ - ' list' : dict like {column -> [values]}
1007
+ - ' series' : dict like {column -> Series(values)}
1008
+ - ' split' : dict like
1009
+ {' index' -> [index], ' columns' -> [columns], ' data' -> [values]}
1010
+ - ' records' : list like
1007
1011
[{column -> value}, ... , {column -> value}]
1008
- - index : dict like {index -> {column -> value}}
1012
+ - ' index' : dict like {index -> {column -> value}}
1009
1013
1010
1014
Abbreviations are allowed. `s` indicates `series` and `sp`
1011
1015
indicates `split`.
@@ -1022,10 +1026,16 @@ def to_dict(self, orient='dict', into=dict):
1022
1026
-------
1023
1027
result : collections.Mapping like {column -> {index -> value}}
1024
1028
1029
+ See Also
1030
+ --------
1031
+ DataFrame.from_dict: create a DataFrame from a dictionary
1032
+ DataFrame.to_json: convert a DataFrame to JSON format
1033
+
1025
1034
Examples
1026
1035
--------
1027
- >>> df = pd.DataFrame(
1028
- {'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b'])
1036
+ >>> df = pd.DataFrame({'col1': [1, 2],
1037
+ ... 'col2': [0.5, 0.75]},
1038
+ ... index=['a', 'b'])
1029
1039
>>> df
1030
1040
col1 col2
1031
1041
a 1 0.50
@@ -1037,16 +1047,19 @@ def to_dict(self, orient='dict', into=dict):
1037
1047
1038
1048
>>> df.to_dict('series')
1039
1049
{'col1': a 1
1040
- b 2
1041
- Name: col1, dtype: int64, 'col2': a 0.50
1042
- b 0.75
1043
- Name: col2, dtype: float64}
1050
+ b 2
1051
+ Name: col1, dtype: int64,
1052
+ 'col2': a 0.50
1053
+ b 0.75
1054
+ Name: col2, dtype: float64}
1055
+
1044
1056
>>> df.to_dict('split')
1045
- {'columns': ['col1', 'col2'],
1046
- 'data': [[1.0, 0.5], [2.0, 0.75]],
1047
- 'index': ['a', 'b']}
1057
+ {'index': ['a', 'b'], ' columns': ['col1', 'col2'],
1058
+ 'data': [[1.0, 0.5], [2.0, 0.75]]}
1059
+
1048
1060
>>> df.to_dict('records')
1049
1061
[{'col1': 1.0, 'col2': 0.5}, {'col1': 2.0, 'col2': 0.75}]
1062
+
1050
1063
>>> df.to_dict('index')
1051
1064
{'a': {'col1': 1.0, 'col2': 0.5}, 'b': {'col1': 2.0, 'col2': 0.75}}
1052
1065
@@ -1055,14 +1068,14 @@ def to_dict(self, orient='dict', into=dict):
1055
1068
>>> from collections import OrderedDict, defaultdict
1056
1069
>>> df.to_dict(into=OrderedDict)
1057
1070
OrderedDict([('col1', OrderedDict([('a', 1), ('b', 2)])),
1058
- ('col2', OrderedDict([('a', 0.5), ('b', 0.75)]))])
1071
+ ('col2', OrderedDict([('a', 0.5), ('b', 0.75)]))])
1059
1072
1060
1073
If you want a `defaultdict`, you need to initialize it:
1061
1074
1062
1075
>>> dd = defaultdict(list)
1063
1076
>>> df.to_dict('records', into=dd)
1064
- [defaultdict(<type 'list'>, {'col2 ': 0.5 , 'col1 ': 1.0 }),
1065
- defaultdict(<type 'list'>, {'col2 ': 0.75 , 'col1 ': 2.0 })]
1077
+ [defaultdict(<class 'list'>, {'col1 ': 1.0 , 'col2 ': 0.5 }),
1078
+ defaultdict(<class 'list'>, {'col1 ': 2.0 , 'col2 ': 0.75 })]
1066
1079
"""
1067
1080
if not self.columns.is_unique:
1068
1081
warnings.warn("DataFrame columns are not unique, some "
0 commit comments