2012-09-26 10 views
5

私はデータフレームを持っている:パンダ階層データフレーム

Form nr Element Type Text Options 
    1 Name1 select text1 op1 
    1 Name1 select text op2 
    1 Name1 select text op3 
    1 Name2 input text2 NaN 
    2 Name1 input text2 NaN 

は、このようなgreateの「ネストされた」階層索引する方法があります:テキスト欄にタイプミスがあると仮定すると、

Form nr Element Type Text Options 
    1 Name1 select text1 op1 
           op2 
           op3 
     Name2 input text2 NaN 
    2 Name1 input text2 NaN 

答えて

13

は、テキスト< - > text1? 私はあなたの最初のDataFrameから行きます。

In [11]: df 
Out[11]: 
    Form nr Element Type Test Options 
0  1  Name1 select text1  op1 
1  1  Name1 select text  op2 
2  1  Name1 select text  op3 
3  1  Name2 input text2  NaN 
4  2  Name1 input text2  NaN 

In [12]: df.set_index(['Form', 'nr Element', 'Type', 'Test']) 
Out[12]: 
          Options 
Form nr Element Type Test   
1 Name1  select text1  op1 
         text  op2 
         text  op3 
    Name2  input text2  NaN 
2 Name1  input text2  NaN 
+1

ありがとうございます。魅力のように働いた。 – root

+0

この回答を説明したり、関連するドキュメントにリンクしたりできますか? – Mike

+0

http://pandas.pydata.org/pandas-docs/stable/advanced.html –