0

I have a situation where I would like to transpose a certain set of columns in my dataframe to rows. The columns range which I want to transpose starts from the fourth column till the last column. There are multiple datasets so one of the datasets can have 10 columns the other one could have 20. The idea is to transpose the columns starting from fourth to the last column.

My example dataset looks like this:

s_date       e_date       response_id   Q1    Q2    Q3    Q4   Q5
12/12/2020   12/12/2020   1             Yes   No    Good  No   Yes

I want this set to transposed as following:

s_date       e_date       response_id   Question_text    Question_Response
12/12/2020   12/12/2020   1             Q1               Yes
16/12/2020   17/12/2020   1             Q2               No
15/12/2020   18/12/2020   1             Q3               Good
14/12/2020   19/12/2020   1             Q4               No
13/12/2020   20/12/2020   1             Q5               Yes

How can I achieve this?

Django0602
  • 797
  • 7
  • 26
  • 2
    `df.melt(id_vars=['s_date','e_date','response_id'], var_name='Question_text', value_name='Question_Response')` – Brendan Dec 28 '22 at 17:36

0 Answers0