0

I have a dataframe like this:

Test A Test B Test C
22 45 24
56 62 52
73 68 11
13 34 96

And I would like transform to:

Test Name Result
Test A 22
Test A 56
Test A 73
Test A 13
Test B 45
Test B 62
Test B 68
Test B 34
Test C 24
Test C 52
Test C 11
Test C 96
Rextein
  • 31
  • 1
  • 6

1 Answers1

1

You can use pandas.melt :

out = df.melt(var_name="Test Name", value_name="Result")

# Output :

print(out)

   Test Name  Result
0     Test A      22
1     Test A      56
2     Test A      73
3     Test A      13
4     Test B      45
..       ...     ...
7     Test B      34
8     Test C      24
9     Test C      52
10    Test C      11
11    Test C      96

[12 rows x 2 columns]
Timeless
  • 22,580
  • 4
  • 12
  • 30
  • Thank you so much. I just can't goolge this method. You can see that my topic(qustion)'s title is not even well defined. My English is not good enough I guess. Do you mind teach me how to google this question? I mean, in this case, what sentence you will use to google this question? – Rextein Dec 11 '22 at 12:18
  • 1
    Hey man, you're welcome. Actually, most of the time, regarding standard data transformation you don't have to google it since pandas has a really good documentation. For example, about reshaping dataframes, there is a big chapter talking about that (including `pandas.melt`) with visualisations. Take a look and good luck ;) : https://pandas.pydata.org/docs/user_guide/reshaping.html – Timeless Dec 11 '22 at 12:22