Pandas“分组”查询HDFStore中的大数据?

2024-05-23 18:49:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个有60多列的HDFStore中有大约700万行。数据超出了我的记忆容量。我希望根据列“a”的值将数据聚合到组中。pandassplitting/aggregating/combining的文档假设我已经在aDataFrame中拥有所有数据,但是我无法将整个存储读取到内存中。在HDFStore中分组数据的正确方法是什么?


Tags: 数据方法记忆内存文档容量combininghdfstore
1条回答
网友
1楼 · 发布于 2024-05-23 18:49:52

这是一个完整的例子。

import numpy as np
import pandas as pd
import os

fname = 'groupby.h5'

# create a frame
df = pd.DataFrame({'A': ['foo', 'foo', 'foo', 'foo',
                         'bar', 'bar', 'bar', 'bar',
                         'foo', 'foo', 'foo'],
                   'B': ['one', 'one', 'one', 'two',
                         'one', 'one', 'one', 'two',
                         'two', 'two', 'one'],
                   'C': ['dull', 'dull', 'shiny', 'dull',
                         'dull', 'shiny', 'shiny', 'dull',
                         'shiny', 'shiny', 'shiny'],
                   'D': np.random.randn(11),
                   'E': np.random.randn(11),
                   'F': np.random.randn(11)})


# create the store and append, using data_columns where I possibily
# could aggregate
with pd.get_store(fname) as store:
    store.append('df',df,data_columns=['A','B','C'])
    print "store:\n%s" % store

    print "\ndf:\n%s" % store['df']

    # get the groups
    groups = store.select_column('df','A').unique()
    print "\ngroups:%s" % groups

    # iterate over the groups and apply my operations
    l = []
    for g in groups:

        grp = store.select('df',where = [ 'A=%s' % g ])

        # this is a regular frame, aggregate however you would like
        l.append(grp[['D','E','F']].sum())


    print "\nresult:\n%s" % pd.concat(l, keys = groups)

os.remove(fname)

输出

store:
<class 'pandas.io.pytables.HDFStore'>
File path: groupby.h5
/df            frame_table  (typ->appendable,nrows->11,ncols->6,indexers->[index],dc->[A,B,C])

df:
      A    B      C         D         E         F
0   foo  one   dull -0.815212 -1.195488 -1.346980
1   foo  one   dull -1.111686 -1.814385 -0.974327
2   foo  one  shiny -1.069152 -1.926265  0.360318
3   foo  two   dull -0.472180  0.698369 -1.007010
4   bar  one   dull  1.329867  0.709621  1.877898
5   bar  one  shiny -0.962906  0.489594 -0.663068
6   bar  one  shiny -0.657922 -0.377705  0.065790
7   bar  two   dull -0.172245  1.694245  1.374189
8   foo  two  shiny -0.780877 -2.334895 -2.747404
9   foo  two  shiny -0.257413  0.577804 -0.159316
10  foo  one  shiny  0.737597  1.979373 -0.236070

groups:Index([bar, foo], dtype=object)

result:
bar  D   -0.463206
     E    2.515754
     F    2.654810
foo  D   -3.768923
     E   -4.015488
     F   -6.110789
dtype: float64

一些注意事项:

1)如果你的团队密度相对较低,这种方法是有意义的。按成百上千个小组的顺序排列。如果你得到的不止这些,就会有更高效(但更复杂的方法),并且你正在应用的函数(在本例中是sum)会变得更严格。

本质上,您可以按块迭代整个存储区,在执行过程中进行分组,但保持这些组只是半折叠的(想象一下执行mean操作,因此您需要保持一个运行总数加上一个运行计数,然后在最后进行除法)。因此,有些操作会比较棘手,但可能会处理许多组(而且速度非常快)。

2)可以通过保存坐标(例如组位置,但这有点复杂)来提高效率

3)此方案不可能进行多分组(这是可能的,但需要更类似于2的方法)

4)要分组的列必须是数据列!

5)你可以在select btw中组合你想要的任何其他过滤器(这是一种sneeky的多组btw方法,你只需在它们的乘积上形成两个唯一的组和迭代器列表,如果你有很多组,效率不是很高,但可以工作)

高温高压

如果这对你有用就告诉我

相关问题 更多 >