使用Pandas进行反规范化数据

3

如果我有以下格式的数据(存储在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 最终,我没有按照最初的意图进行去规范化,而是仅遍历了感兴趣的列。但任何能够更好地去规范化的能力都可以使其更好。

你能展示一下你当前的代码吗? - Jeff
相关链接:https://dev59.com/3GQn5IYBdhLWcg3wETvj#17116976 - Dan Allan
1个回答

1

首先,我强烈建议不要一开始就像这样存储数据,pandas并不是为像列表这样的通用对象而设计的。

以下是提取数据的一种方法(使用连接,类似于Dan Allen的答案)。

def denormalise(df, *colnames):
    df1 = df.copy() #  optional, but means we're not changing df globally
    cols = [(colname, df1.pop(colname).apply(pd.Series).stack()) for colname in colnames]
    for colname, c in cols:
        c.index = c.index.droplevel(-1)
        c.name = colname
        df1 = df1.join(c)
    return df1
    # optionally .reindex_axis(df.columns, axis=1)  # reorder columns
    #        and .reset_index(drop=True)            # 0,1,...n index

正在使用中:

In [11]: denormalise(df1, 'wares')
Out[11]:
              categories           slug             wares
0  [developer, mac, web]     alex.payne       macbook-pro
0  [developer, mac, web]     alex.payne    cinema-display
0  [developer, mac, web]     alex.payne  readynas-nv-plus
1        [mac, musician]  jona.bechtolt   audio-kontrol-1
1        [mac, musician]  jona.bechtolt  powershot-sd1000
1        [mac, musician]  jona.bechtolt              live

Pandas允许您将此作为DataFrame方法添加,以方便使用:
In [12]: pd.DataFrame.denormalise = denormalise

In [13]: df1.denormalise('wares', 'categories')
Out[13]:
            slug             wares categories
0     alex.payne       macbook-pro  developer
0     alex.payne       macbook-pro        mac
0     alex.payne       macbook-pro        web
0     alex.payne    cinema-display  developer
0     alex.payne    cinema-display        mac
0     alex.payne    cinema-display        web
0     alex.payne  readynas-nv-plus  developer
0     alex.payne  readynas-nv-plus        mac
0     alex.payne  readynas-nv-plus        web
1  jona.bechtolt   audio-kontrol-1        mac
1  jona.bechtolt   audio-kontrol-1   musician
1  jona.bechtolt  powershot-sd1000        mac
1  jona.bechtolt  powershot-sd1000   musician
1  jona.bechtolt              live        mac
1  jona.bechtolt              live   musician

In [14]: df1.denormalise('wares', 'categories').reset_index(drop=True)
Out[14]:
             slug             wares categories
0      alex.payne       macbook-pro  developer
1      alex.payne       macbook-pro        mac
2      alex.payne       macbook-pro        web
3      alex.payne    cinema-display  developer
4      alex.payne    cinema-display        mac
5      alex.payne    cinema-display        web
6      alex.payne  readynas-nv-plus  developer
7      alex.payne  readynas-nv-plus        mac
8      alex.payne  readynas-nv-plus        web
9   jona.bechtolt   audio-kontrol-1        mac
10  jona.bechtolt   audio-kontrol-1   musician
11  jona.bechtolt  powershot-sd1000        mac
12  jona.bechtolt  powershot-sd1000   musician
13  jona.bechtolt              live        mac
14  jona.bechtolt              live   musician

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接