pandas - multiindex level output additional row -


i trying set multiindex simple pandas dataframe. first index type of shop , second type of fruit. expecting see 2 groups shop1 , shop2 first column have ended three, shop1, shop2 , shop1 again. why happening?

area2 = pd.dataframe({'01/01/2017': [2000, 2500, 100, 1600],                 '01/02/2017': [2000, 2500, 50, 1000],                 '01/03/2017': [2000, 500, 50, 1600,],                 '01/04/2017': [2500, 2000, 0, 1600],                 'fruit': ['apples', 'banana', 'pears', 'b/berry'],                 'shop': ['shop1', 'shop2', 'shop1', 'shop1']})  s2 = area2.set_index(['shop', 'fruit']) 

current output

                01/01/2017  01/02/2017  01/03/2017  01/04/2017 shop    fruit                shop1   apples    2000       2000       2000         2500 shop2   banana    2500       2500       500          2000 shop1   pears     100        50         50           0         b/berry   1600       1000       1600         1600 

what expecting

               01/01/2017   01/02/2017  01/03/2017  01/04/2017 shop    fruit                shop1   apples    2000       2000       2000         2500         pears     100        50         50           0         b/berry   1600       1000       1600         1600 shop2   banana    2500       2500       500          2000 

i think need sort_index sorting multiindex:

df = s2.sort_index() print (df)                01/01/2017  01/02/2017  01/03/2017  01/04/2017 shop  fruit                                                   shop1 apples         2000        2000        2000        2500       pears           100          50          50           0       b/berry        1600        1000        1600        1600 shop2 banana         2500        2500         500        2000 

but first level of multiindex not showing same consecutive data default.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -