Ray Ray - 1 month ago 6
Python Question

Creating a multiindexed `Series` with a nested dictionary

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?

Answer

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