Reputation: 9
this code is same my code except i have 300 columns, i need to reorder columns like this: Total provit, Catgory ,numberofgoodsold for each company. it easy if i have few columns but as i said i have 300.
data = {'company': ['AMC', 'ER','CRR' , 'TYU'], 'Reg-ID': ['1222','2334','3444', '4566'], 'Total_provit': ['123300','12233', '3444444', '412222'], 'numberofgoodsold':['44','23','67','34'], Catgory: ['Tal','ERP','RTY close','ALL']}
d = pd.DataFrame(data)
d2 = d.pivot(index = 'Reg-ID', columns = 'company')
d2.columns = d2.columns.swaplevel(0, 1)
d2.sort_index(axis=1, level=0, inplace=True)
Upvotes: 0
Views: 268
Reputation: 6665
What about using pandas.DataFrame.reindex
as follows
>>> desired_order = ['Total_provit', 'Catgory', 'numberofgoodsold']
>>> d2.reindex(desired_order, level=1, axis=1)
company AMC ... TYU
Total_provit Catgory ... Catgory numberofgoodsold
Reg-ID ...
1222 123300 Tal ... NaN NaN
2334 NaN NaN ... NaN NaN
3444 NaN NaN ... NaN NaN
4566 NaN NaN ... ALL 34
Upvotes: 1