熊猫分层数据帧

roo*_*oot 5 python dataframe pandas

我有一个数据帧:

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
Run Code Online (Sandbox Code Playgroud)

有没有办法像这样更新"嵌套"层次索引:

Form nr Element Type    Text    Options
   1    Name1   select  text1   op1
                                op2
                                op3
        Name2   input   text2   NaN
   2    Name1   input   text2   NaN
Run Code Online (Sandbox Code Playgroud)

Wou*_*ire 14

假设文本列中有拼写错误,文字< - > 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
Run Code Online (Sandbox Code Playgroud)