Python language contains a large of number libraries, that make highly complicated tasks very easy to achieve. Pandas is one such library and is used for data manipulation and analysis. To be specific, It includes operations and data structures for manipulating numerical tables and time series. In this article, we will learn how to convert series (a data structure specific to Pandas) to dictionary in Python. But before going straight to the methods, let us have a quick recap of the series and dictionary data structure.
What is Pandas Series?
It is essentially a one-dimensional, axis-labeled data structure that is available only in the Pandas library. Series can store all types of data such as strings, integer, float, and other python objects. Each element in this data structure has a label associated with it, and all the labels collectively are referred to as an index. Note that, Series data structure is just like the array data structure with one basic difference. In arrays, indexes are integers beginning from 0, but in series, the index can be anything even strings. The labels do not need to be unique but they must be of hashable type.
What is a Dictionary in Python?
Dictionary is a data structure in python, which stores information in the form of key-value pairs. It is written in curly braces consisting of keys and values. When the key is known, dictionaries are optimized to retrieve values. Note that duplicate keys are allowed in a dictionary.
How to Convert Pandas Series to Dictionary in Python?
Dictionaries allow us to quickly find values if the keys are known. If we have a pandas Series with some relevant index, then we can convert it to a dictionary object containing “index: value” key-value pairs to effectively find values by using the corresponding index.
We can achieve this task by using a method called Series.to_dict(). This method is an inbuilt method present in the Series class of the Pandas module. The method syntax is as follows
Series.to_dict(into=<class 'dict'>)
This method takes the Series object which we want to convert as its argument and returns the Key-value representation of the passed Series. Let us now look at a few examples to see this function in action.
For example:
# importing pandas as pd import pandas as pd # Creating the Series using default index sr1 = pd.Series(['Apple', 'Orange', 'Banana', 'Grapes', 'Brinjal']) # Creating the Series using custom index index = ["Red", "Orange", "Yellow", "Green", "Purple"] sr2 = pd.Series(['Apple', 'Orange', 'Banana', 'Grapes', 'Brinjal'], index = index) # Convert to dictionary dict1 = sr1.to_dict() dict2 = sr2.to_dict() print("Series with default indexes") print(dict1) print("Series with custom indexes") print(dict2)
Output:
Series with default indexes {0: 'Apple', 1: 'Orange', 2: 'Banana', 3: 'Grapes', 4: 'Brinjal'} Series with custom indexes {'Red': 'Apple', 'Orange': 'Orange', 'Yellow': 'Banana', 'Green': 'Grapes', 'Purple': 'Brinjal'}
Conclusion
A good programmer knows his or her way around conversions between different types of objects. In this article, we first had a brief talk about Pandas Series and Dictionaries. After that, we finally saw the conversion using the to_dict() method. If you are planning to work in the Data Science or Machine Learning domain then we strongly recommend you to have a hold on such types of conversions.