bigframes.pandas.DataFrame.to_dict#

DataFrame.to_dict(orient: ~typing.Literal['dict', 'list', 'series', 'split', 'tight', 'records', 'index'] = 'dict', into: type[dict] = <class 'dict'>, *, allow_large_results: bool | None = None, **kwargs) dict | list[dict][source]#

Convert the DataFrame to a dictionary.

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

Examples:

>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
>>> df.to_dict()
{'col1': {np.int64(0): 1, np.int64(1): 2}, 'col2': {np.int64(0): 3, np.int64(1): 4}}

You can specify the return orientation.

>>> df.to_dict('series')
{'col1': 0    1
1    2
Name: col1, dtype: Int64,
'col2': 0    3
1    4
Name: col2, dtype: Int64}
>>> df.to_dict('split')
{'index': [0, 1], 'columns': ['col1', 'col2'], 'data': [[1, 3], [2, 4]]}
>>> df.to_dict("tight")
{'index': [0, 1],
'columns': ['col1', 'col2'],
'data': [[1, 3], [2, 4]],
'index_names': [None],
'column_names': [None]}
Parameters:
  • orient (str {'dict', 'list', 'series', 'split', 'tight', 'records', 'index'}) – Determines the type of the values of the dictionary. ‘dict’ (default) : dict like {column -> {index -> value}}. ‘list’ : dict like {column -> [values]}. ‘series’ : dict like {column -> Series(values)}. split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}. ‘tight’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values], ‘index_names’ -> [index.names], ‘column_names’ -> [column.names]}. ‘records’ : list like [{column -> value}, … , {column -> value}]. ‘index’ : dict like {index -> {column -> value}}.

  • 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.

  • index (bool, default True) – Whether to include the index item (and index_names item if orient is ‘tight’) in the returned dictionary. Can only be False when orient is ‘split’ or ‘tight’.

  • allow_large_results (bool, default None) – If not None, overrides the global setting to allow or disallow large query results over the default size limit of 10 GB.

Returns:

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

Return type:

dict or list of dict