问题:如何创建允许在熊猫HDFStore对象中对多个列进行迭代的生成器?
我正在尝试为一个熊猫HDFStore对象创建一个包装类。我正在尝试实现的特性之一是能够按给定的块大小迭代HDFStore中的列组。许多机器学习算法可以操作on-line,而不需要同时使用所有的数据。
我的第一次尝试是创建一个生成器函数,并将开始和停止参数传递给HDFStore的select方法:
def iterate(self, key, chunksize=50000):
node = self.store.get_node(key)
nrows = node.table.nrows
current = 0
while current < nrows:
yield self.store.select(key, start=current, stop=current+chunksize)
current += chunksize这很好,而且我能够在存储中的单个存储列上迭代。注意,为了测试,我将每一列存储在它自己的表中。
我的下一步是使用HDFStore.select_as_multiple将这个概念扩展到多个表的多个列。虽然没有在docstring中,但select_as_multiple似乎也接受开始和停止参数:
>>> store.select_as_multiple(keys='MachineID', start=0, stop=50000)
<class 'pandas.core.frame.DataFrame'>
Int64Index: 50000 entries, 0 to 49999
Data columns:
MachineID 50000 non-null values
dtypes: int64(1)根据请求,只返回50 000行。但是,当我传递超过一个键/列时,该方法会将所有行撤回:
>>> store.select_as_multiple(keys=['MachineID','YearMade'], start=0, stop=50000)
<class 'pandas.core.frame.DataFrame'>
Int64Index: 401125 entries, 0 to 1124
Data columns:
MachineID 401125 non-null values
YearMade 401125 non-null values
dtypes: int64(2)是否可以使用select_as_multiple来提取指定范围的行而不是所有行?
版本信息:
>>> pd.__version__
'0.10.1'
>>> tables.__version__
'2.4.0'发布于 2013-03-17 18:55:44
它不能工作,因为没有将开始/停止传递给基础选择。很容易解决。
还意味着添加迭代器支持,以窃取您的功能:)
done https://github.com/pydata/pandas/issues/3078
有文档,但本质上是:
for df in store.select('df',chunksize=10000):
print dfhttps://stackoverflow.com/questions/15463183
复制相似问题