0

I have a Pandas dataframe(df1) like below

Job Machine command Time
One Moon sleep 12:00
Two Jupiter python 13:00

I want it to look like the below so I can use df1.to_html (the top line isn't supposed to be header, I'm just not sure how to edit table without that)

Job One
Machine Moon
command Sleep
Time 12:00
Job Two
Machine Jupiter
Command python
Time 13:00

I have tried to df1.T to transpose it, and it looks like below which is good, but I couldn't find a way to duplicate indexes and move the columns

Job One TWO
Machine Moon Jupiter
command Sleep python
Time 12:00 13:00

I then tried stack df1.T.stack() which looks like below, but doesn't have its own separate indexes.

Job One
Two
Machine Moon
Jupiter
Command sleep
python
Time 12:00
13:00

I was then looking at pd.wide_to_long, but I didn't seem to get anywhere with that.

This was closed and linked to melt however I don't seem to see melt working? Melt turns out similar to stack

Job One
Job Two
Machine Moon
Machine Jupiter
Command sleep
Command python
Time 12:00
Time 13:00

This is not the same as table 2 which I was looking for.

I have tried df1 =pd.melt(df1.T) and that nearly gets what I need, but I am left with variable without the index?

variable value
0 One
0 Moon
0 Sleep
0 12:00
1 Two
1 Jupiter
1 python
1 13:00

So, the right column is what I want, but left column has lost its index. Job / Machine / Command / Time / Job /Machine / Command / Time etc.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Demo
  • 25
  • 4
  • Not sure what the issue is with my question. Its valid and should be allowed. I have found the answer by trial and error - df1 = pd.melt(df1.T, ignore_index = False) – Demo Oct 10 '22 at 23:01
  • I was then left with index, variable column, value. I just deleted the variable column and all looked great – Demo Oct 11 '22 at 09:08

0 Answers0