2

I have a dataframe like this:

pd.DataFrame(data={"a": [1,2], "b": [3,4], "c": [5,6]}, index=[0,1])

In tabular form:

   a  b  c
0  1  3  5
1  2  4  6

I want to transform it to a dataframe like this:

pd.DataFrame(data={"foo":[1,2,3,4,5,6]},
             index=pd.MultiIndex.from_tuples(
               [('a', 0), ('a', 1), ('b', 0), ('b', 1), ('c', 0), ('c', 1)],
               names=("var", "id")))

tabular form:

         foo
var id     
a   0      1
    1      2
b   0      3
    1      4
c   0      5
    1      6

What is the easiest way to achieve this?

2 Answers 2

2

Try unstack

out = df.unstack().to_frame('foo')
Out[146]: 
     foo
a 0    1
  1    2
b 0    3
  1    4
c 0    5
  1    6
Sign up to request clarification or add additional context in comments.

2 Comments

wow much better than mine
@Umar.H yours good as well ~ :-)
2

First create a new multiindex

df.columns = pd.MultiIndex.from_product([df.columns.tolist(), ['foo']])

print(df)

    a   b   c
  foo foo foo
0   1   3   5
1   2   4   6

then use .stack with .swaplevel()

df.stack(0).swaplevel(0,1)

     foo
a 0    1
b 0    3
c 0    5
a 1    2
b 1    4
c 1    6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.