2016-10-26 9 views
1

私の考えでは、私がやろうとしていることは、コンストラクタに渡すのと同じように単純であるべきですが、実際はそうではありません。私は以下のような辞書を持っています。ネストされた辞書でマルチインデックス化された `Series`を作成する

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)}} 

私はpandas.Series(d)ような何かを行うと以下のようなSeriesインスタンスを取得したいと思います。

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

しかし実際には以下のようになります。

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

私が達成しようとしているようなものを達成する方法はありますか?

答えて

1

私はあなたがunstackDataFrameコンストラクタが必要だと思う:

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 

またMultiIndex使用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 
関連する問題