Friday 15 June 2012

python - Multi-Index Sorting in Pandas -


I have a multi-index dataframe that is created through a group-operation. I'm trying to sort a composite using several levels of the index, but I think what I want is that one can get the function in a way.

The initial dataset looks like this (daily sales count of different products): product name of the product product launch date sales 0 0 0 0 0 0 0 0 1 1 3 4 5 13 2 2013-01-01 Samsung Galaxy 2009-04-27 14 3 2013-01-01 Samsung Galaxy Tab 2010-09-02 15 4 2013-01-02 Apple iPod 2001-10-23 22 5 2013-01-02 Apple iPad 2010-04-03 17 6/2013-01-02 Samsung Galaxy 2009-04-27 10 7/2013-01-02 Samsung Galaxy Tab 2010-09-02 7 < / Pre>

I use the amount over the date range I The group said:

  & gt; Grouped = df.groupby (('Productive', 'Product Name', 'Date of Product Launch']). Zodiac () Sales Manufacturer Product Name Product Launch Date Apple iPad 2010-04-03 30 iPod 2001-10- 23 34 Samsung Galaxy 2009-04-27 24 Galaxy Tab 2010-09-02 22   

So far so good!

The last thing I want to do now is , It's like every manufacturer after launching the products, but grouping them under the manufacturer in order - here I am Trying to:

  Sales Producer Product Name Product Launch Date Apple iPod 2001-10-23 34 iPad 2010-04 -03 30 Samsung Galaxy 2009-04-27 24 Galaxy Tab: 2010-09-02 22   

When I try SLEVEL () I had previously lost good per-company hierarchy:

  & gt; Group Headlawl ('Product Launch Date') Sales Manufacturer Product Name Product Launch Date Apple iPod 2001-10-23 34 Sam Galaxy 2012-04-27 24 Apple iPad 2010-04-03 30 Samsung Galaxy Tab 2010-09-02 22   

sort () and sort_index () just failed:

  grouped.sort (['Manufacturer', 'Product Launch Date']) KeyError: There is no item named 'Manufacturer' Productive Group. KeyIare: Manufacturer's name 'item' looks like a simple operation, but I can not understand it. Source = Index (By = [['Manufacturer', 'Date of Product Launch]] Keyyer:  

I am not obliged to use a multi index for this, but since then the returns are returned by the group (), so I am working with it.

The code for the production of initial dataframe is BTD:

  data = {'date': ['2013-01-01', '2013-01-01' , '2013 -01-01', '2013-01-01', '2013-01-02', '2013-01-02', '2013-01-02', '2013-01-02'], 'Manufacturer': ['' App '', '' '' 'Galaxy Tab' '', '' '' '', '' '', '', 'Samsung', 'Samsung', 'Apple', 'Apple', 'Samsung', 'Samsung',], 'Product Name' , 'IPad', 'iPad', 'Galaxy', 'Galaxy Tab'], 'Product Launch Date': ['2001-10-23', 'PD', 'iPad', 'Galaxy', 'Galaxy Tab' 2010-04-03 ',' 2009-04-27 ',' 2010-09-02 ',' 2001-10-23 ',' 2010-04-03 ',' 2009-04-27 ',' 2010- 09-02 '],' Sales': [12, 13, 14, 15, 22, 17 , 10, 7]} df = DataFrame (data, column = ['date', 'creator', 'product name' 'product launch date', 'cells'])    

will be a hack to change the order of the sequence:

  in [11]: g out [ 11]: Sales Manufacturer Product Name Product Launch Date Apple iPad 2010-04-03 30 iPod 2001-10-23 34 Samsung Galaxy 2009-04-27 24 in Galaxy Tab 2010-09-02 22 at [12]: g. Index = g.index.swaplevel (1, 2)   

Sorleval, which (as you have found) Sorts level:

 in  [13]: g = g.sortlevel ()   

and swap back: < In [14]: g.index = g.index.swaplevel (1, 2): [15]: Out [15]: Sales Manufacturer Product Name Product Launch Date Apple iPod 2001-10-23 34 IPad 2010-04-03 30 Samsung Galaxy 2009-04-27 24 Galaxy Tab 2010-09-02 22

I am of the opinion that Sortelval is sorted in the remaining label order Should not, therefore a gythenbo problem will arise. :) Although it is important to mention the document about it.

Note: You can first escape from the code> Swapelvel by reordering the order of the initial group:

  g = df.groupby ( ['Manufacturer', 'Product Launch Date', 'Product Name']). ()    

No comments:

Post a Comment