【问题标题】:Collapse multi-index pandas折叠多索引 pandas
【发布时间】:2016-01-19 15:09:30
【问题描述】:

我正在尝试折叠多索引表,以便折叠后的值出现在新列中。任何人都可以提供一些帮助吗?非常感谢。

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]})
+------------+--------------+-------+------------+-- ------------------+----------------+---------+-- -----+------------+--------------+--------------+--- ------+-------+--------+ |产品编号 |产品名称 |颜色 |品牌 |制造成本|运输成本 |单位 |季节性|市场|旺季|关系 |托运人 |销售 |警报 | +------------+--------------+-------+------------+-- ------------------+----------------+---------+-- -----+------------+--------------+--------------+--- ------+-------+--------+ | 1 |足球 |黑色 |萨迪达 | 0.004893964 | 0.026574225 |美元 |错误 |西 |夏天|独家 |极致 | 0.58 |真的 | | 1 |足球 |黑色 |萨迪达 | 0.004893964 | 0.026574225 |美元 |错误 |西南 |夏天|独家 |极致 | 0.23 |错误 | | 1 |足球 |黑色 |萨迪达 | 0.004893964 | 0.026574225 |美元 |错误 |东 |夏天|独家 |极致 | 0.23 |真的 | | 2 |篮球 |红色 |晕圈 | 6.287116543 | 34.13904283 |美分 |真的 |东 |弹簧 |独家 |极致 | 0.1 |错误 | | 2 |篮球 |红色 |晕圈 | 6.287116543 | 34.13904283 |美分 |真的 |南 |弹簧 |独家 |极致 | 0.04 |真的 | | 2 |篮球 |红色 |晕圈 | 6.287116543 | 34.13904283 |美分 |真的 |东北|弹簧 |独家 |极致 | 0.02 |错误 | | 3 |足球 |红色 |诺斯利夫 | 16.19975422 | 87.9646654 |美分 |真的 |东北|秋季 |独家 |极致 | 0.06 |真的 | | 3 |足球 |红色 |诺斯利夫 | 16.19975422 | 87.9646654 |美分 |真的 |南 |秋季 |独家 |极致 | 0.15 |错误 | | 3 |足球 |红色 |诺斯利夫 | 16.19975422 | 87.9646654 |美分 |真的 |东 |秋季 |独家 |极致 | 0.03 |真的 | | 1 |足球 |黑色 |萨迪达 | 0.006019576 | 0.032686297 |美元 |错误 |西 |夏天|独家 |埃姆卡 | 0.71 |真的 | | 1 |足球 |黑色 |萨迪达 | 0.006019576 | 0.032686297 |美元 |错误 |西南 |夏天|独家 |埃姆卡 | 0.28 |错误 | | 1 |足球 |黑色 |萨迪达 | 0.006019576 | 0.032686297 |美元 |错误 |东 |夏天|独家 |埃姆卡 | 0.28 |真的 | | 2 |篮球 |红色 |晕圈 | 7.733153347 | 41.99102268 |美分 |真的 |东 |弹簧 |独家 |埃姆卡 | 0.13 |错误 | | 2 |篮球 |红色 |晕圈 | 7.733153347 | 41.99102268 |美分 |真的 |南 |弹簧 |独家 |埃姆卡 | 0.05 |真的 | | 2 |篮球 |红色 |晕圈 | 7.733153347 | 41.99102268 |美分 |真的 |东北|弹簧 |独家 |埃姆卡 | 0.03 |错误 | | 3 |足球 |红色 |诺斯利夫 | 19.92569769 | 108.1965384 |美分 |真的 |东北|秋季 |独家 |埃姆卡 | 0.08 |真的 | | 3 |足球 |红色 |诺斯利夫 | 19.92569769 | 108.1965384 |美分 |真的 |南 |秋季 |独家 |埃姆卡 | 0.19 |错误 | | 3 |足球 |红色 |诺斯利夫 | 19.92569769 | 108.1965384 |美分 |真的 |东 |秋季 |独家 |埃姆卡 | 0.04 |真的 | +------------+--------------+-------+------------+-- ------------------+--------------+----------+-- -----+-----------+--------------+--------------+--- ------+-------+--------+

致以下:

+------------+--------------+-------+------------+-- ------------------+--------------+----------+-- -----+-------------+--------------+---------+----- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---+ |产品编号 |产品名称 |颜色 |品牌 |制造成本|运输成本 |单位 |季节性|旺季|关系 |托运人 |地位 +------------+--------------+-------+------------+-- ------------------+--------------+----------+-- -----+-------------+--------------+---------+----- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---+ | 1 |足球 |黑色 |萨迪达 | 0.004893964 | 0.026574225 |美元 |错误 |夏天|独家 |极致 | "{"West"": {"sales": 0.58, "alert": True}, "Southwest": {"sales": 0.23, "alert": False}, "East": {"sales": 0.23, “警报”:真}}“ | 2 |篮球 |红色 |晕圈 | 6.287116543 | 34.13904283 |美分 |真的 |弹簧 |独家 |极致 | "{"East" : {"sales" : 0.1, "alert" : True}, "South" : {"sales" : 0.04, "alert" : False}, "Northeast" : {"sales" : 0.02, "警报“:真}}” | 3 |足球 |红色 |诺斯利夫 | 16.19975422 | 87.9646654 |美分 |真的 |秋季 |独家 |极致 | “{“Northeast”:{“sales”:0.06,“alert”:True},“South”:{“sales”:0.15,“alert”:False},“East”:{“sales”:0.03,“警报“:真}}” | 1 |足球 |黑色 |萨迪达 | 0.006019576 | 0.032686297 |美元 |错误 |夏天|独家 |埃姆卡 | “{“West”:{“sales”:0.71,“alert”:True},“Southwest”:{“sales”:0.28,“alert”:False},“East”:{“sales”:0.28,“警报“:真}}” | 2 |篮球 |红色 |晕圈 | 7.733153347 | 41.99102268 |美分 |真的 |弹簧 |独家 |埃姆卡 | “{“West”:{“sales”:0.13,“alert”:True},“Southwest”:{“sales”:0.05,“alert”:False},“East”:{“sales”:0.03, “警报”:真}}“ | 3 |足球 |红色 |诺斯利夫 | 19.92569769 | 108.1965384 |美分 |真的 |秋季 |独家 |埃姆卡 | “{“West”:{“sales”:0.08,“alert”:True},“Southwest”:{“sales”:0.19,“alert”:False},“East”:{“sales”:0.04,”警报“:真}}” +------------+--------------+-------+------------+-- ------------------+--------------+----------+-- -----+-------------+--------------+---------+----- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---+

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以将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')
    

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 1970-01-01
      • 2021-06-24
      • 2012-07-20
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      相关资源
      最近更新 更多