Skip to content

Commit 79200d3

Browse files
CheuktingTomAugspurger
authored andcommitted
DOC: update the docstring of pandas.DataFrame.to_dict (#20162)
1 parent 281ce99 commit 79200d3

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

pandas/core/frame.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -991,21 +991,25 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None):
991991
return cls(data, index=index, columns=columns, dtype=dtype)
992992

993993
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).
995999

9961000
Parameters
9971001
----------
9981002
orient : str {'dict', 'list', 'series', 'split', 'records', 'index'}
9991003
Determines the type of the values of the dictionary.
10001004

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
10071011
[{column -> value}, ... , {column -> value}]
1008-
- index : dict like {index -> {column -> value}}
1012+
- 'index' : dict like {index -> {column -> value}}
10091013

10101014
Abbreviations are allowed. `s` indicates `series` and `sp`
10111015
indicates `split`.
@@ -1022,10 +1026,16 @@ def to_dict(self, orient='dict', into=dict):
10221026
-------
10231027
result : collections.Mapping like {column -> {index -> value}}
10241028

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+
10251034
Examples
10261035
--------
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'])
10291039
>>> df
10301040
col1 col2
10311041
a 1 0.50
@@ -1037,16 +1047,19 @@ def to_dict(self, orient='dict', into=dict):
10371047

10381048
>>> df.to_dict('series')
10391049
{'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+
10441056
>>> 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+
10481060
>>> df.to_dict('records')
10491061
[{'col1': 1.0, 'col2': 0.5}, {'col1': 2.0, 'col2': 0.75}]
1062+
10501063
>>> df.to_dict('index')
10511064
{'a': {'col1': 1.0, 'col2': 0.5}, 'b': {'col1': 2.0, 'col2': 0.75}}
10521065

@@ -1055,14 +1068,14 @@ def to_dict(self, orient='dict', into=dict):
10551068
>>> from collections import OrderedDict, defaultdict
10561069
>>> df.to_dict(into=OrderedDict)
10571070
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)]))])
10591072

10601073
If you want a `defaultdict`, you need to initialize it:
10611074

10621075
>>> dd = defaultdict(list)
10631076
>>> 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})]
10661079
"""
10671080
if not self.columns.is_unique:
10681081
warnings.warn("DataFrame columns are not unique, some "

0 commit comments

Comments
 (0)