0

I have a dataset where I would like to pivot the entire dataframe, using certain columns as values.

Data

id  date    sun moon    stars   total   ppp base    final   final2
aa  Q1 21   5   1       2       8       0   200     41      5
aa  Q2 21   4   1       2       7       1   200     50      6

Desired

id  date    type    ppp base    final   final2
aa  Q1 21   sun     0   200     41      5
aa  Q1 21   moon    0   200     41      5
aa  Q1 21   stars   0   200     41      5
aa  Q2 21   sun     1   200     50      6
aa  Q2 21   moon    1   200     50      6
aa  Q2 21   stars   1   200     50      6

Doing

df.pivot(index='foo', columns='bar', values='baz')

(sample code)

I believe I have to perform something similar to this, but not exactly sure how to arrange this. I am still researching, any suggestion is appreciated.

Lynn
  • 4,292
  • 5
  • 21
  • 44
  • 1
    `df.drop(columns='total').melt(['id','date','final','final2','base','ppp'],var_name='type',value_name='ppp')` – Anurag Dabas Aug 12 '21 at 04:29
  • 1
    Yes @AnuragDabas I will try this . The melt function is creating a long dataset I believe – Lynn Aug 12 '21 at 04:30

0 Answers0