1

With this dataframe:

+--------+---------+------+----------+
| metric | control | test | duration |
+--------+---------+------+----------+
| logins | 4       | 2    | short    |
+--------+---------+------+----------+

I want to transpose the control and test column values into multiple rows under their own column called value, but I also want to consolidate the control and test columns into one column called cohort. The result would then be:

+--------+---------+-------+----------+
| metric | cohort  | value | duration |
+--------+---------+-------+----------+
| logins | control | 4     | short    |
+--------+---------+-------+----------+
| logins | test    | 2     | short    |
+--------+---------+-------+----------+

I know this involves a melt or pivot function, but I can't figure out how to do it.

bbk611
  • 321
  • 2
  • 10
  • 1
    You are likely looking for [pd.melt](https://pandas.pydata.org/docs/reference/api/pandas.melt.html) : ``df.melt(['metric', 'duration'], var_name='cohort')`` – sammywemmy Oct 26 '21 at 04:56

0 Answers0