pandas 1.4.2

ParametersReturnsBackRef
to_dict(self, orient: 'str' = 'dict', into=<class 'dict'>)

The type of the key-value pairs can be customized with the parameters (see below).

Parameters

orient : str {'dict', 'list', 'series', 'split', 'records', 'index'}

Determines the type of the values of the dictionary.

into : class, default dict

The collections.abc.Mapping subclass used for all Mappings in the return value. Can be the actual class or an empty instance of the mapping type you want. If you want a collections.defaultdict, you must pass it initialized.

Returns

dict, list or collections.abc.Mapping

Return a collections.abc.Mapping object representing the DataFrame. The resulting transformation depends on the :None:None:`orient` parameter.

Convert the DataFrame to a dictionary.

See Also

DataFrame.from_dict

Create a DataFrame from a dictionary.

DataFrame.to_json

Convert a DataFrame to JSON format.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({'col1': [1, 2],
...  'col2': [0.5, 0.75]},
...  index=['row1', 'row2'])
... df col1 col2 row1 1 0.50 row2 2 0.75
This example is valid syntax, but we were not able to check execution
>>> df.to_dict()
{'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 0.5, 'row2': 0.75}}

You can specify the return orientation.

This example is valid syntax, but we were not able to check execution
>>> df.to_dict('series')
{'col1': row1    1
         row2    2
Name: col1, dtype: int64,
'col2': row1    0.50
        row2    0.75
Name: col2, dtype: float64}
This example is valid syntax, but we were not able to check execution
>>> df.to_dict('split')
{'index': ['row1', 'row2'], 'columns': ['col1', 'col2'],
 'data': [[1, 0.5], [2, 0.75]]}
This example is valid syntax, but we were not able to check execution
>>> df.to_dict('records')
[{'col1': 1, 'col2': 0.5}, {'col1': 2, 'col2': 0.75}]
This example is valid syntax, but we were not able to check execution
>>> df.to_dict('index')
{'row1': {'col1': 1, 'col2': 0.5}, 'row2': {'col1': 2, 'col2': 0.75}}
This example is valid syntax, but we were not able to check execution
>>> df.to_dict('tight')
{'index': ['row1', 'row2'], 'columns': ['col1', 'col2'],
 'data': [[1, 0.5], [2, 0.75]], 'index_names': [None], 'column_names': [None]}

You can also specify the mapping type.

This example is valid syntax, but we were not able to check execution
>>> from collections import OrderedDict, defaultdict
... df.to_dict(into=OrderedDict) OrderedDict([('col1', OrderedDict([('row1', 1), ('row2', 2)])), ('col2', OrderedDict([('row1', 0.5), ('row2', 0.75)]))])

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

This example is valid syntax, but we were not able to check execution
>>> dd = defaultdict(list)
... df.to_dict('records', into=dd) [defaultdict(<class 'list'>, {'col1': 1, 'col2': 0.5}), defaultdict(<class 'list'>, {'col1': 2, 'col2': 0.75})]
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.common.standardize_mapping

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#1762
type: <class 'function'>
Commit: