1

this is the Dataframe

Year    Category    Performance
2014    Sales           1000
2014    Expenses        400
2014    Profit          200
2015    Sales           1170
2015    Expenses        460
2015    Profit          250
2016    Sales           660
2016    Expenses        1120
2016    Profit          300

And this is the expected output

 [          
          ['Year', 'Sales', 'Expenses', 'Profit'],          
          ['2014', 1000, 400, 200],         
          ['2015', 1170, 460, 250],         
          ['2016', 660, 1120, 300],                     
        ]           

or if you could help me to convert this dataframe into this

Year Sales Expenses Profit      
2014 1000  400       200        
2015 1170  460       250        
2016 660   1120      300
  • 1
    Looks like a duplicate to: https://stackoverflow.com/questions/17298313/python-pandas-convert-rows-as-column-headers – Zephyrus Nov 06 '19 at 13:14

1 Answers1

1
df = pd.pivot_table(df, index='Year', columns='Category', values='Performence').reset_index()
Aryerez
  • 3,417
  • 2
  • 9
  • 17
  • 1
    Code only answers can be helpful, but 99% of the time they are better if you add some explanation. Could you describe what your code does to help readers understand it? – 3limin4t0r Nov 06 '19 at 15:37
  • @3limin4t0r I don't think a simple one-line one-function (and I don't count `reset_index()` as a real function) needs explainations. If someone doesn't understand a function, he can simply google it. – Aryerez Nov 06 '19 at 17:37