Creating a multiindexed `Series` with a nested dictionary

╄→гoц情女王★ 提交于 2019-12-11 08:09:35

问题


In my mind, what I'm trying to do ought to be straightforward, as straightforward as passing it into the constructor, but in reality it's not. I have a dictionary like below.

d = {"russell": {"score": numpy.random.rand(), "ping": numpy.random.randint(10, 100)},
    "cantor": {"score": numpy.random.rand(), "ping": numpy.random.randint(10, 100)},
    "godel": {"score": numpy.random.rand(), "ping": numpy.random.randint(10, 100)}}

I would like to do something like pandas.Series(d) and get a Series instance like below.

russell  score  0.87391482
         ping   23
cantor   score  0.77821932
         ping   16
godel    score  0.53372128
         ping   35

But what I actually get is below.

cantor     {'ping': 44, 'score': 0.007408727109865398}
godel        {'ping': 41, 'score': 0.9338940910283948}
russell       {'ping': 74, 'score': 0.733817307366666}

Is there a way to achieve something like what I'm trying to achieve?


回答1:


I think you need DataFrame constructor with unstack:

import pandas as pd
import numpy as np

d = {"russell": {"score": np.random.rand(), "ping": np.random.randint(10, 100)},
    "cantor": {"score": np.random.rand(), "ping": np.random.randint(10, 100)},
    "godel": {"score": np.random.rand(), "ping": np.random.randint(10, 100)}}

print (pd.DataFrame(d).unstack())  

cantor   ping     33.000000
         score     0.240253
godel    ping     64.000000
         score     0.435040
russell  ping     41.000000
         score     0.171810
dtype: float64

Also if need swap levels in MultiIndex use stack:

print (pd.DataFrame(d).stack())    
ping   cantor     64.000000
       godel      40.000000
       russell    66.000000
score  cantor      0.265771
       godel       0.283725
       russell     0.085856
dtype: float64


来源:https://stackoverflow.com/questions/40261031/creating-a-multiindexed-series-with-a-nested-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!