Skip to content

Commit fe60a54

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

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

pandas/core/frame.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,11 @@ 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 the parameters (see below).
928932
929933
Parameters
930934
----------
@@ -949,16 +953,19 @@ def to_dict(self, orient='dict', into=dict):
949953
instance of the mapping type you want. If you want a
950954
collections.defaultdict, you must pass it initialized.
951955
952-
.. versionadded:: 0.21.0
956+
.. versionadded:: 0.21.0.
953957
954958
Returns
955959
-------
956960
result : collections.Mapping like {column -> {index -> value}}
957961
962+
See Also
963+
--------
964+
from_dict: create a DataFrame from a dictionary
965+
958966
Examples
959967
--------
960-
>>> df = pd.DataFrame(
961-
{'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b'])
968+
>>> df = pd.DataFrame({'col1':[1, 2],'col2':[0.5, 0.75]},index=['a', 'b'])
962969
>>> df
963970
col1 col2
964971
a 1 0.50
@@ -975,9 +982,8 @@ def to_dict(self, orient='dict', into=dict):
975982
b 0.75
976983
Name: col2, dtype: float64}
977984
>>> df.to_dict('split')
978-
{'columns': ['col1', 'col2'],
979-
'data': [[1.0, 0.5], [2.0, 0.75]],
980-
'index': ['a', 'b']}
985+
{'index': ['a', 'b'], 'columns': ['col1', 'col2'],
986+
'data': [[1.0, 0.5], [2.0, 0.75]]}
981987
>>> df.to_dict('records')
982988
[{'col1': 1.0, 'col2': 0.5}, {'col1': 2.0, 'col2': 0.75}]
983989
>>> df.to_dict('index')
@@ -994,8 +1000,8 @@ def to_dict(self, orient='dict', into=dict):
9941000
9951001
>>> dd = defaultdict(list)
9961002
>>> 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})]
1003+
[defaultdict(<class 'list'>, {'col1': 1.0, 'col2': 0.5}),
1004+
defaultdict(<class 'list'>, {'col1': 2.0, 'col2': 0.75})]
9991005
"""
10001006
if not self.columns.is_unique:
10011007
warnings.warn("DataFrame columns are not unique, some "

0 commit comments

Comments
 (0)