折叠多指数熊猫

时间:2016-01-19 05:22:32

标签: python pandas

我正在尝试折叠多索引表,以便折叠的值显示在新列中。有人可以提供一些帮助吗?非常感谢。

import pandas

df = pd.DataFrame({'product_id' : [1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3],
              'product_name' : ['soccer ball', 'soccer ball', 'soccer ball', 'basketball', 'basketball', 'basketball', 'football', 'football', 'football', 'soccer ball', 'soccer ball', 'soccer ball', 'basketball', 'basketball', 'basketball', 'football', 'football', 'football'],
              'color' : ['black', 'black', 'black', 'red', 'red', 'red', 'red', 'red', 'red', 'black', 'black', 'black', 'red', 'red', 'red', 'red', 'red', 'red'],
              'brand' : ['sadida', 'sadida', 'sadida', 'gnidlaps', 'gnidlaps', 'gnidlaps', 'nosliw', 'nosliw', 'nosliw', 'sadida', 'sadida', 'sadida', 'gnidlaps', 'gnidlaps', 'gnidlaps', 'nosliw', 'nosliw', 'nosliw'],
              'manufacturing_cost' : [0.004893964, 0.004893964, 0.004893964, 6.287116543, 6.287116543, 6.287116543, 16.19975422, 16.19975422, 16.19975422, 0.006019576, 0.006019576, 0.006019576, 7.733153347, 7.733153347, 7.733153347, 19.92569769, 19.92569769, 19.92569769],
              'shipping_cost' : [0.026574225, 0.026574225, 0.026574225, 34.13904283, 34.13904283, 34.13904283, 87.9646654, 87.9646654, 87.9646654, 0.032686297, 0.032686297, 0.032686297, 41.99102268, 41.99102268, 41.99102268, 108.1965384, 108.1965384, 108.1965384],
              'units' : ['dollars', 'dollars', 'dollars', 'cents', 'cents', 'cents', 'cents', 'cents', 'cents', 'dollars', 'dollars', 'dollars', 'cents', 'cents', 'cents', 'cents', 'cents', 'cents'],
              'seasonal' : [False, False, False, True, True, True, True, True, True, False, False, False, True, True, True, True, True, True],
              'market' : ['West', 'Southwest', 'East', 'East', 'South', 'Northeast', 'Northeast', 'South', 'East', 'West', 'Southwest', 'East', 'East', 'South', 'Northeast', 'Northeast', 'South', 'East'],
              'high_season' : ['summer', 'summer', 'summer', 'spring', 'spring', 'spring', 'fall', 'fall', 'fall', 'summer', 'summer', 'summer', 'spring', 'spring', 'spring', 'fall', 'fall', 'fall'],
              'relationship' : ['exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive', 'exclusive'],
              'shipper' : ['Acme', 'Acme', 'Acme', 'Acme', 'Acme', 'Acme', 'Acme', 'Acme', 'Acme', 'Emca', 'Emca', 'Emca', 'Emca', 'Emca', 'Emca', 'Emca', 'Emca', 'Emca'],
              'sales' : [0.58, 0.23, 0.23, 0.1, 0.04, 0.02, 0.06, 0.15, 0.03, 0.71, 0.28, 0.28, 0.13, 0.05, 0.03, 0.08, 0.19, 0.04],
              'alert' : [True, False, True, False, True, False, True, False, True, True, False, True, False, True, False, True, False, True]})
+------------+--------------+-------+----------+--------------------+---------------+---------+----------+-----------+-------------+--------------+---------+-------+-------+
| product_id | product_name | color |  brand   | manufacturing_cost | shipping_cost |  units  | seasonal |  market   | high_season | relationship | shipper | sales | alert |
+------------+--------------+-------+----------+--------------------+---------------+---------+----------+-----------+-------------+--------------+---------+-------+-------+
|          1 | soccer ball  | black | sadida   | 0.004893964        | 0.026574225   | dollars | FALSE    | West      | summer      | exclusive    | Acme    | 0.58  | TRUE  |
|          1 | soccer ball  | black | sadida   | 0.004893964        | 0.026574225   | dollars | FALSE    | Southwest | summer      | exclusive    | Acme    | 0.23  | FALSE |
|          1 | soccer ball  | black | sadida   | 0.004893964        | 0.026574225   | dollars | FALSE    | East      | summer      | exclusive    | Acme    | 0.23  | TRUE  |
|          2 | basketball   | red   | gnidlaps | 6.287116543        | 34.13904283   | cents   | TRUE     | East      | spring      | exclusive    | Acme    | 0.1   | FALSE |
|          2 | basketball   | red   | gnidlaps | 6.287116543        | 34.13904283   | cents   | TRUE     | South     | spring      | exclusive    | Acme    | 0.04  | TRUE  |
|          2 | basketball   | red   | gnidlaps | 6.287116543        | 34.13904283   | cents   | TRUE     | Northeast | spring      | exclusive    | Acme    | 0.02  | FALSE |
|          3 | football     | red   | nosliw   | 16.19975422        | 87.9646654    | cents   | TRUE     | Northeast | fall        | exclusive    | Acme    | 0.06  | TRUE  |
|          3 | football     | red   | nosliw   | 16.19975422        | 87.9646654    | cents   | TRUE     | South     | fall        | exclusive    | Acme    | 0.15  | FALSE |
|          3 | football     | red   | nosliw   | 16.19975422        | 87.9646654    | cents   | TRUE     | East      | fall        | exclusive    | Acme    | 0.03  | TRUE  |
|          1 | soccer ball  | black | sadida   | 0.006019576        | 0.032686297   | dollars | FALSE    | West      | summer      | exclusive    | Emca    | 0.71  | TRUE  |
|          1 | soccer ball  | black | sadida   | 0.006019576        | 0.032686297   | dollars | FALSE    | Southwest | summer      | exclusive    | Emca    | 0.28  | FALSE |
|          1 | soccer ball  | black | sadida   | 0.006019576        | 0.032686297   | dollars | FALSE    | East      | summer      | exclusive    | Emca    | 0.28  | TRUE  |
|          2 | basketball   | red   | gnidlaps | 7.733153347        | 41.99102268   | cents   | TRUE     | East      | spring      | exclusive    | Emca    | 0.13  | FALSE |
|          2 | basketball   | red   | gnidlaps | 7.733153347        | 41.99102268   | cents   | TRUE     | South     | spring      | exclusive    | Emca    | 0.05  | TRUE  |
|          2 | basketball   | red   | gnidlaps | 7.733153347        | 41.99102268   | cents   | TRUE     | Northeast | spring      | exclusive    | Emca    | 0.03  | FALSE |
|          3 | football     | red   | nosliw   | 19.92569769        | 108.1965384   | cents   | TRUE     | Northeast | fall        | exclusive    | Emca    | 0.08  | TRUE  |
|          3 | football     | red   | nosliw   | 19.92569769        | 108.1965384   | cents   | TRUE     | South     | fall        | exclusive    | Emca    | 0.19  | FALSE |
|          3 | football     | red   | nosliw   | 19.92569769        | 108.1965384   | cents   | TRUE     | East      | fall        | exclusive    | Emca    | 0.04  | TRUE  |
+------------+--------------+-------+----------+--------------------+---------------+---------+----------+-----------+-------------+--------------+---------+-------+-------+

以下内容:


+------------+--------------+-------+----------+--------------------+---------------+---------+----------+-------------+--------------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| product_id | product_name | color |  brand   | manufacturing_cost | shipping_cost |  units  | seasonal | high_season | relationship | shipper |                                                                            status                                                                    
+------------+--------------+-------+----------+--------------------+---------------+---------+----------+-------------+--------------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
|          1 | soccer ball  | black | sadida   | 0.004893964        | 0.026574225   | dollars | FALSE    | summer      | exclusive    | Acme    | "{"West"" : {"sales" : 0.58, "alert" : True}, "Southwest" : {"sales" : 0.23, "alert" : False}, "East" : {"sales" : 0.23, "alert" : True}}"
|          2 | basketball   | red   | gnidlaps | 6.287116543        | 34.13904283   | cents   | TRUE     | spring      | exclusive    | Acme    | "{"East" : {"sales" : 0.1, "alert" : True}, "South" : {"sales" : 0.04, "alert" : False}, "Northeast" : {"sales" : 0.02, "alert" : True}}"
|          3 | football     | red   | nosliw   | 16.19975422        | 87.9646654    | cents   | TRUE     | fall        | exclusive    | Acme    | "{"Northeast" : {"sales" : 0.06, "alert" : True}, "South" : {"sales" : 0.15, "alert" : False}, "East" : {"sales" : 0.03, "alert" : True}}"
|          1 | soccer ball  | black | sadida   | 0.006019576        | 0.032686297   | dollars | FALSE    | summer      | exclusive    | Emca    | "{"West" : {"sales" : 0.71, "alert" : True}, "Southwest" : {"sales" : 0.28, "alert" : False}, "East" : {"sales" : 0.28, "alert" : True}}"
|          2 | basketball   | red   | gnidlaps | 7.733153347        | 41.99102268   | cents   | TRUE     | spring      | exclusive    | Emca    | "{"West" : {"sales" : 0.13, "alert"" : True}, "Southwest" : {"sales" : 0.05, "alert" : False}, "East" : {"sales" : 0.03, "alert" : True}}"
|          3 | football     | red   | nosliw   | 19.92569769        | 108.1965384   | cents   | TRUE     | fall        | exclusive    | Emca    | "{"West" : {"sales" : 0.08, "alert" : True}, "Southwest" : {"sales" : 0.19, "alert" : False}, "East" : {"sales" : 0.04,"alert" : True}}"
+------------+--------------+-------+----------+--------------------+---------------+---------+----------+-------------+--------------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 个答案:

答案 0 :(得分:0)

您可以将groupbyto_dictset_indexreset_index一起使用:

print df.groupby(['product_id',
                  'product_name',
                  'color',
                  'brand',
                  'manufacturing_cost',
                  'shipping_cost',
                  'units',
                  'seasonal',
                  'high_season',
                  'relationship',
                  'shipper'])[["market", "sales","alert"]]
         .apply(lambda x: x.set_index('market').to_dict(orient='index') )
         .reset_index(name='status')