Skip to content

Commit 6afa51e

Browse files
Moisandatapythonista
authored andcommitted
DOC: Updating docstring of DataFrame.to_dict (#22827)
1 parent cf11f71 commit 6afa51e

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

ci/code_checks.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
118118

119119
MSG='Doctests frame.py' ; echo $MSG
120120
pytest --doctest-modules -v pandas/core/frame.py \
121-
-k"-axes -combine -itertuples -join -nlargest -nsmallest -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack -to_dict -to_stata"
121+
-k"-axes -combine -itertuples -join -nlargest -nsmallest -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack -to_stata"
122122
RET=$(($RET + $?)) ; echo $MSG "DONE"
123123

124124
MSG='Doctests series.py' ; echo $MSG

pandas/core/frame.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -1146,51 +1146,53 @@ def to_dict(self, orient='dict', into=dict):
11461146
11471147
Returns
11481148
-------
1149-
result : collections.Mapping like {column -> {index -> value}}
1149+
dict, list or collections.Mapping
1150+
Return a collections.Mapping object representing the DataFrame.
1151+
The resulting transformation depends on the `orient` parameter.
11501152
11511153
See Also
11521154
--------
1153-
DataFrame.from_dict: create a DataFrame from a dictionary
1154-
DataFrame.to_json: convert a DataFrame to JSON format
1155+
DataFrame.from_dict: Create a DataFrame from a dictionary.
1156+
DataFrame.to_json: Convert a DataFrame to JSON format.
11551157
11561158
Examples
11571159
--------
11581160
>>> df = pd.DataFrame({'col1': [1, 2],
11591161
... 'col2': [0.5, 0.75]},
1160-
... index=['a', 'b'])
1162+
... index=['row1', 'row2'])
11611163
>>> df
1162-
col1 col2
1163-
a 1 0.50
1164-
b 2 0.75
1164+
col1 col2
1165+
row1 1 0.50
1166+
row2 2 0.75
11651167
>>> df.to_dict()
1166-
{'col1': {'a': 1, 'b': 2}, 'col2': {'a': 0.5, 'b': 0.75}}
1168+
{'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 0.5, 'row2': 0.75}}
11671169
11681170
You can specify the return orientation.
11691171
11701172
>>> df.to_dict('series')
1171-
{'col1': a 1
1172-
b 2
1173-
Name: col1, dtype: int64,
1174-
'col2': a 0.50
1175-
b 0.75
1176-
Name: col2, dtype: float64}
1173+
{'col1': row1 1
1174+
row2 2
1175+
Name: col1, dtype: int64,
1176+
'col2': row1 0.50
1177+
row2 0.75
1178+
Name: col2, dtype: float64}
11771179
11781180
>>> df.to_dict('split')
1179-
{'index': ['a', 'b'], 'columns': ['col1', 'col2'],
1181+
{'index': ['row1', 'row2'], 'columns': ['col1', 'col2'],
11801182
'data': [[1.0, 0.5], [2.0, 0.75]]}
11811183
11821184
>>> df.to_dict('records')
11831185
[{'col1': 1.0, 'col2': 0.5}, {'col1': 2.0, 'col2': 0.75}]
11841186
11851187
>>> df.to_dict('index')
1186-
{'a': {'col1': 1.0, 'col2': 0.5}, 'b': {'col1': 2.0, 'col2': 0.75}}
1188+
{'row1': {'col1': 1, 'col2': 0.5}, 'row2': {'col1': 2, 'col2': 0.75}}
11871189
11881190
You can also specify the mapping type.
11891191
11901192
>>> from collections import OrderedDict, defaultdict
11911193
>>> df.to_dict(into=OrderedDict)
1192-
OrderedDict([('col1', OrderedDict([('a', 1), ('b', 2)])),
1193-
('col2', OrderedDict([('a', 0.5), ('b', 0.75)]))])
1194+
OrderedDict([('col1', OrderedDict([('row1', 1), ('row2', 2)])),
1195+
('col2', OrderedDict([('row1', 0.5), ('row2', 0.75)]))])
11941196
11951197
If you want a `defaultdict`, you need to initialize it:
11961198

0 commit comments

Comments
 (0)