2

I want to convert this dataframe (note that 'ABC' is the index name):

       t1    t2    t3 
ABC
gp      7    11    26
fp      6    14    23
pm      3    -1     7
wm      2    -2     9

to this dataframe:

     s1   tx    gp   fp   pm   wm 
0   ABC   t1     7    6    3    2
1   ABC   t2    11   14   -1   -2
2   ABC   t3    26   23    7    9

What's the best way to perform this function?

1 Answer 1

3

How about this:

df = df.T
df.reset_index(inplace=True)
df.rename(columns={"index":"tx"}, inplace=True)
df["s1"] = "ABC"
Sign up to request clarification or add additional context in comments.

1 Comment

df.T.rename_axis(‘tx’).reset_index().assign(s1=df.index.name) I think putting iloc[:, np.r_[-1, :-1]] at the end might work to reorder the columns

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.