如果我有以下格式的数据(存储在pandas数据帧中),基本上是将类别和商品的规范形式转换为slug:
pandas.DataFrame:
categories slug wares
0 [developer, mac, web] alex.payne [macbook-pro, cinema-display, readynas-nv-plus...
1 [mac, musician] jona.bechtolt [audio-kontrol-1, powershot-sd1000, live, mda-...
2 [game, suit, windows] gabe.newell [oa-desk, beyond-tv, windows-xp, office, visua...
3 [developer, mac, software] steven.frank [mac-pro, macbook-air, apple-tv, itunes, addre...
我的意图是绘制与商品相关的类别图表,我需要以非规范化格式获取数据,格式如下:
categories wares slug
0 developer macbook-pro alex.payne
1 mac macbook-pro alex.payne
2 web macbook-pro alex.payne
3 developer cinema-display alex.payne
4 mac cinema-display alex.payne
5 web cinema-display alex.payne
6 developer readynas-nv-plus alex.payne
什么是将上述格式的数据转换为下面格式的最佳方法,最好使用numpy内部的方法,以便速度更快。我的方法相对较为天真,遍历数据帧中的每一行,维护元组列表,然后将其传递给pandas.DataFrame构造函数。你可能会有更好、更快的建议,因此请提出!我还在思考这种数据在pandas DataFrame中的替代表示方式,特别是稀疏矩阵。但我认为这对于分组查询来说会更好。如果还有其他格式或者如果稀疏矩阵对于这种聚合查询来说更好,请建议如何进行操作。对于那些感兴趣的人,这里是整个内容:http://j.mp/lp-usesthis 最终,我没有按照最初的意图进行去规范化,而是仅遍历了感兴趣的列。但任何能够更好地去规范化的能力都可以使其更好。