HomeLinuxPandas to Dictionary

Pandas to Dictionary


A dictionary is a manner of storing info in Python. It has keys and values which are associated to one another. Typically in Python, we might need to convert Pandas DataFrame into a distinct format, reminiscent of a dictionary. The “DataFrame.to_dict()” methodology converts the DataFrame to a Dictionary in Python.

This tutorial will provide you with an in depth information on changing Pandas to Dictionaries utilizing quite a few examples.

Convert Pandas DataFrame to Python Dictionary?

To transform/remodel a specific Pandas DataFrame to Dictionary, the “DataFrame.to_dict()” methodology is utilized in Python. The “DataFrame.to_dict()” syntax is proven beneath:

DataFrame.to_dict(orient=‘dict’, into=<class ‘dict’>, index=True)

Right here within the above syntax:

  • The “orient” parameter specifies the kind of worth of the dictionary. It may be one of many following strings: “dict”, “record”, “collection”, “break up”, “tight”, “data”, or “index”.
  • The “into” parameter specifies the “collections.abc.Mapping” subclass utilized for all mappings within the retrieved worth. (Default is “dict”).
  • The “index” parameter determines whether or not so as to add the index component to the returned dictionary.

The “DataFrame.to_dict()” methodology will return a dictionary with the column names as keys and the column values as sub-dictionaries with the index labels as keys.

Instance 1: Convert Pandas DataFrame to Dictionary

This instance makes use of the “df.to_dict()” methodology to transform Pandas DataFrame to Dictionary in Python:

import pandas

dict1 = {‘title’: [‘Joseph’, ‘Anna’, ‘Lily’], ‘id’: [12, 22, 33], ‘wage’: [‘$334’, ‘$348’, ‘$583’]}

df = pandas.DataFrame(dict1)

print(‘DataFrame:n, df)

print(nDictionary Worth:n, df.to_dict())

Within the above code:

  • The “pandas” module is imported.
  • The “pd.DataFrame()” creates the DataFrame by accepting the dictionary Information as an argument.
  • The “df.to_dict()” methodology converts the Pandas DataFrame to Dictionary.

Output

The dictionary has been created from the Pandas DataFrame.

Instance 2: Convert Pandas DataFrame to Dictionary Utilizing “orient” Parameter

This instance code is used to transform DataFrame to Dictionary utilizing the required “orient” parameter worth reminiscent of record or break up:

import pandas

dict1 = {‘title’: [‘Joseph’, ‘Anna’, ‘Lily’], ‘id’: [12, 22, 33], ‘wage’: [‘$334’, ‘$348’, ‘$583’]}

df = pandas.DataFrame(dict1)

print(‘DataFrame:n, df)

print(nDictionary Worth:n, df.to_dict(orient=‘record’))

Within the above code, the “orient” parameter worth “record” is handed to the “df.to_dict()” methodology to transform the Pandas DataFrame to the dictionary within the “record” orientation construction.

Output

The dictionary has been returned within the “record” orientation.

Conclusion

In Python, the “DataFrame.to_dict()” methodology of the “pandas” module is used to transform the required Pandas DataFrame to Dictionary. The “DataFrame.to_dict()” methodology takes the “orient” parameter as an argument and returns the DataFrame in a specified orientation reminiscent of “record”, “break up”, and so forth. This text introduced/delivered an intensive information on Pandas to Dictionaries using quite a few examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments