0

I have a multiindexed (3 column levels) pandas. Dataframe which is something like this:

       t1                      t2
       p1          p2          p1          p2
       v1    v2    v1    v2    v1    v2    v1    v2

       A     B     C     D     E     F     G     H

I need to rearrange it to:

       t1         t2
       v1   v2    v1    v2

 p1    A    B     E     F
 p2    C    D     G     H

But when I try:

df.stack(level=1)

I get:

ValueError: cannot reindex from a duplicate axis

This implies that the repetition of labels makes this operation impossible, but that doesn't make sense to me, because otherwise there would be no reason to run .stack().

  1. Why do I get this error? I imagine I'm not understanding the linked answer.
  2. How can I achieve the desired outcome?
Community
  • 1
  • 1
user1318135
  • 717
  • 2
  • 12
  • 36
  • Why not use `pivot`: http://pandas.pydata.org/pandas-docs/stable/reshaping.html ? – Serenity Jul 18 '16 at 11:56
  • What version of pandas are you using? And can you provide a reproducible example? If I try to replicate this with `df = pd.DataFrame([list('ABCDEFGH')], columns=pd.MultiIndex.from_product([['t1','t2'],['p1','p2'],['v1','v2']]))`, then it works fine for me. – joris Jul 18 '16 at 12:37

0 Answers0