Home > OS >  Python Pandas pivot tables format
Python Pandas pivot tables format

Time:10-02

I have made a pivot table using this

piv_full = pd.pivot_table(full_stock_list, index='Unique_Location', values=['StockID', 'SoldStatus'],
                          columns='date_of_file', aggfunc={'StockID': lambda x: len(x.unique()), 'SoldStatus': np.sum})

The output looks something like this: Pivot table output

Is it possible to make it look something like this: Pivot table I want

CodePudding user response:

Try:

piv_full.unstack(level=0).swaplevel().sort_index()

CodePudding user response:

I got this to work with some dummy data that mirrored your own:

piv_full.stack(level=0)
  • Related