@@ -1146,51 +1146,53 @@ def to_dict(self, orient='dict', into=dict):
1146
1146
1147
1147
Returns
1148
1148
-------
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.
1150
1152
1151
1153
See Also
1152
1154
--------
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.
1155
1157
1156
1158
Examples
1157
1159
--------
1158
1160
>>> df = pd.DataFrame({'col1': [1, 2],
1159
1161
... 'col2': [0.5, 0.75]},
1160
- ... index=['a ', 'b '])
1162
+ ... index=['row1 ', 'row2 '])
1161
1163
>>> 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
1165
1167
>>> 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}}
1167
1169
1168
1170
You can specify the return orientation.
1169
1171
1170
1172
>>> 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}
1177
1179
1178
1180
>>> df.to_dict('split')
1179
- {'index': ['a ', 'b '], 'columns': ['col1', 'col2'],
1181
+ {'index': ['row1 ', 'row2 '], 'columns': ['col1', 'col2'],
1180
1182
'data': [[1.0, 0.5], [2.0, 0.75]]}
1181
1183
1182
1184
>>> df.to_dict('records')
1183
1185
[{'col1': 1.0, 'col2': 0.5}, {'col1': 2.0, 'col2': 0.75}]
1184
1186
1185
1187
>>> 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}}
1187
1189
1188
1190
You can also specify the mapping type.
1189
1191
1190
1192
>>> from collections import OrderedDict, defaultdict
1191
1193
>>> 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)]))])
1194
1196
1195
1197
If you want a `defaultdict`, you need to initialize it:
1196
1198
0 commit comments