Skip to content

Commit a4121e7

Browse files
author
Cheuk Ting Ho
committed
DOC: update the docstring of pandas.DataFrame.to_dict
1 parent d7bcb22 commit a4121e7

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

pandas/core/frame.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,12 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None):
924924
return cls(data, index=index, columns=columns, dtype=dtype)
925925

926926
def to_dict(self, orient='dict', into=dict):
927-
"""Convert DataFrame to dictionary.
927+
"""
928+
Convert DataFrame to dictionary.
929+
930+
Method converting the DataFrame to a Python dictionary,
931+
the type of the key-value pairs can be customized with
932+
the parameters (see below).
928933
929934
Parameters
930935
----------
@@ -949,16 +954,21 @@ def to_dict(self, orient='dict', into=dict):
949954
instance of the mapping type you want. If you want a
950955
collections.defaultdict, you must pass it initialized.
951956
952-
.. versionadded:: 0.21.0
957+
.. versionadded:: 0.21.0.
953958
954959
Returns
955960
-------
956961
result : collections.Mapping like {column -> {index -> value}}
957962
963+
See Also
964+
--------
965+
from_dict: create a DataFrame from a dictionary
966+
958967
Examples
959968
--------
960-
>>> df = pd.DataFrame(
961-
{'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b'])
969+
>>> df = pd.DataFrame({'col1': [1, 2],
970+
... 'col2': [0.5, 0.75]},
971+
... index=['a', 'b'])
962972
>>> df
963973
col1 col2
964974
a 1 0.50
@@ -975,9 +985,8 @@ def to_dict(self, orient='dict', into=dict):
975985
b 0.75
976986
Name: col2, dtype: float64}
977987
>>> df.to_dict('split')
978-
{'columns': ['col1', 'col2'],
979-
'data': [[1.0, 0.5], [2.0, 0.75]],
980-
'index': ['a', 'b']}
988+
{'index': ['a', 'b'], 'columns': ['col1', 'col2'],
989+
'data': [[1.0, 0.5], [2.0, 0.75]]}
981990
>>> df.to_dict('records')
982991
[{'col1': 1.0, 'col2': 0.5}, {'col1': 2.0, 'col2': 0.75}]
983992
>>> df.to_dict('index')
@@ -994,8 +1003,8 @@ def to_dict(self, orient='dict', into=dict):
9941003
9951004
>>> dd = defaultdict(list)
9961005
>>> df.to_dict('records', into=dd)
997-
[defaultdict(<type 'list'>, {'col2': 0.5, 'col1': 1.0}),
998-
defaultdict(<type 'list'>, {'col2': 0.75, 'col1': 2.0})]
1006+
[defaultdict(<class 'list'>, {'col1': 1.0, 'col2': 0.5}),
1007+
defaultdict(<class 'list'>, {'col1': 2.0, 'col2': 0.75})]
9991008
"""
10001009
if not self.columns.is_unique:
10011010
warnings.warn("DataFrame columns are not unique, some "

0 commit comments

Comments
 (0)