covnert a dataframe serie in a dictionnary in python

You can convert a pandas series to a dictionary using the .to_dict() method. Here's an example:

main.py
import pandas as pd

# create a pandas series
my_series = pd.Series([10, 20, 30, 40, 50])

# convert series to dictionary
my_dict = my_series.to_dict()

print(my_dict)   # output: {0: 10, 1: 20, 2: 30, 3: 40, 4: 50}
216 chars
10 lines

In the above example, we first create a pandas series called my_series. We then use the .to_dict() method to convert the series into a dictionary and store the result in my_dict. Finally, we print the resulting dictionary.

The resulting dictionary has keys that correspond to the index of the series, and values that correspond to the values in the series.

gistlibby LogSnag