To convert a pandas Series to a dictionary in Python, the easiest way by using to_dict() on a pandas Series.
import pandas as pd
df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"],
"gender":["F","F","F","F","M","M","M","F","M"],
"age":[1,2,3,4,5,6,7,8,9],
"weight":[10,20,15,20,25,10,15,30,40]})
animal_types = df["animal_type"].to_dict()
print(animal_types)
#Output:
{0: 'dog', 1: 'cat', 2: 'dog', 3: 'cat', 4: 'dog', 5: 'dog', 6: 'cat', 7: 'cat', 8: 'dog'}
If you don’t want this format, you can use to_dict() on the entire DataFrame which gives you have a few different orientation options: ‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’. Then you can access the values you want after the conversion.
import pandas as pd
df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"],
"gender":["F","F","F","F","M","M","M","F","M"],
"age":[1,2,3,4,5,6,7,8,9],
"weight":[10,20,15,20,25,10,15,30,40]})
df_as_dict = df.to_dict(orient="list")
print(df_as_dict)
print(df_as_dict["animal_type"])
#Output:
{'animal_type': ['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog'], 'gender': ['F', 'F', 'F', 'F', 'M', 'M', 'M', 'F', 'M'], 'age': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'weight': [10, 20, 15, 20, 25, 10, 15, 30, 40]}
{'animal_type': ['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog']}
You can also use dict() function to convert a pandas Series to a dictionary.
import pandas as pd
df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"],
"gender":["F","F","F","F","M","M","M","F","M"],
"age":[1,2,3,4,5,6,7,8,9],
"weight":[10,20,15,20,25,10,15,30,40]})
animal_types = dict(df["animal_type"])
print(animal_types)
#Output:
{0: 'dog', 1: 'cat', 2: 'dog', 3: 'cat', 4: 'dog', 5: 'dog', 6: 'cat', 7: 'cat', 8: 'dog'}
When working with collections of data, the ability to be able to easily access certain pieces of information is valuable.
One such situation is if you want to get the values of a pandas Series and create a dictionary in Python.
There are a few ways you can create a dictionary from the values of a pandas Series.
Having the values of a Series in a list can be useful if you want to loop over the values and perform an action.
To convert a pandas Series to a dictionary in Python, the easiest way by using to_dict() on a pandas Series.
Below shows a simple example of how you can create a dictionary from the values of a pandas Series with to_dict() in Python.
import pandas as pd
df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"],
"gender":["F","F","F","F","M","M","M","F","M"],
"age":[1,2,3,4,5,6,7,8,9],
"weight":[10,20,15,20,25,10,15,30,40]})
animal_types = df["animal_type"].to_dict()
print(animal_types)
#Output:
{0: 'dog', 1: 'cat', 2: 'dog', 3: 'cat', 4: 'dog', 5: 'dog', 6: 'cat', 7: 'cat', 8: 'dog'}
If you don’t want this format, you can use to_dict() on the entire DataFrame which gives you have a few different orientation options: ‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’. Then you can access the values you want after the conversion.
Below is an example of orienting the converted DataFrame as a dictionary so that the values of each column are in a list with ‘list’ and then accessing the “animal_type” to get the values of “animal_type”.
import pandas as pd
df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"],
"gender":["F","F","F","F","M","M","M","F","M"],
"age":[1,2,3,4,5,6,7,8,9],
"weight":[10,20,15,20,25,10,15,30,40]})
df_as_dict = df.to_dict(orient="list")
print(df_as_dict)
print(df_as_dict["animal_type"])
#Output:
{'animal_type': ['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog'], 'gender': ['F', 'F', 'F', 'F', 'M', 'M', 'M', 'F', 'M'], 'age': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'weight': [10, 20, 15, 20, 25, 10, 15, 30, 40]}
{'animal_type': ['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog']}
Using dict() to Convert pandas Series to Dictionary in Python
Another way you can convert the pandas Series values to a dictionary is with the Python dict() function.
dict() tries to convert a Python object to a dictionary. The dictionary representation of a pandas Series is each key/value pair is the index and value of the Series.
Below shows another example of how you can get the Series values of a pandas Series as a dictionary in Python.
import pandas as pd
df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"],
"gender":["F","F","F","F","M","M","M","F","M"],
"age":[1,2,3,4,5,6,7,8,9],
"weight":[10,20,15,20,25,10,15,30,40]})
animal_types = dict(df["animal_type"])
print(animal_types)
#Output:
{0: 'dog', 1: 'cat', 2: 'dog', 3: 'cat', 4: 'dog', 5: 'dog', 6: 'cat', 7: 'cat', 8: 'dog'}
Hopefully this article has been useful for you to be able to learn how to convert pandas Series to a dictionary in Python.