w3resource

Pandas DataFrame: to_dict() function

DataFrame - to_dict() function

The to_dict() function is used to convert the DataFrame to a dictionary.

Syntax:

DataFrame.to_dict(self, orient='dict', into=<class 'dict'>)

Parameters:

Name Description Type / Default Value Required / Optional
orient    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]}
  • 'records' : list like [{column -> value}, … , {column -> value}]
  • 'index' : dict like {index -> {column -> value}}
Abbreviations are allowed. s indicates series and sp indicates split.
str {'dict', 'list', 'series', 'split', 'records', 'index'} Required
into 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. class
Default Value: dict
Required

Returns: dict, list or collections.abc.Mapping
Return a collections.abc.Mapping object representing the DataFrame. The resulting transformation depends on the orient parameter.

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - to_sql() function
Next: DataFrame - to_excel() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/dataframe/dataframe-to_dict.php