I have the below table in excel (xls):
Quarter Q1 Q1 Q1 Year 2013 2014 2015 Company A A A PA 500 300 963 NY 200 150 1500 What I would like to create is the below table by using Python Pandas:
Quarter Year State Company Amount Q1 2013 PA A 500 Q1 2014 PA A 300 Q1 2015 PA A 963 Q1 2013 NY A 200 Q1 2014 NY A 150 Q1 2015 NY A 1500 Edit: I have used the below code per @jezrael suggestion:
import pandas as p df = p.read_excel('test.xls') df = df.set_index('0') df = df.T.stack().reset_index(name='idx') df.columns = ['Quarter', 'Year', 'Company', 'State', 'Amount'] df = df[['Quarter', 'Year', 'State', 'Company', 'Amount']] df = df.sort_values(by=['State'], ascending=['False']).reset_index(drop=True) print(df) I kept getting the below error:
File "pandas\hashtable.pyx", line 676, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12216) KeyError: '0' I have also tried with 0, the same error with KeyError: 0