How can I drop the elements of this dictionary into rows

Viewed 65
1 Answers

To add the contents of this dictionary to rows, you should first access the row with the dictionary (s.data), then use s.data.items() to get the key and value pair that will be used to create the new Series. The previous step results in a dict_item which can then be converted into a list, after that, assign the first (and only) tuple of that list to name and values. These variables will then be used with pd.Series to create the named Series.

import pandas as pd

s = pd.Series(
    index=["data"],
    data=[{'Huobi':[0.01, 0.036944, 0.033314, 0.023049]}],
    name="dataMap"
)
print(s)

name, values = list(s.data.items())[0]
s_out = pd.Series(name=name, data=values)
print(s_out)

Output from s

data    {'Huobi': [0.01, 0.036944, 0.033314, 0.023049]}
Name: dataMap, dtype: object

Output from s_out

0    0.010000
1    0.036944
2    0.033314
3    0.023049
Name: Huobi, dtype: float64
Related