Pandas稀疏数据帧在磁盘上比密集版本更大

时间:2014-02-06 18:16:00

标签: python pandas sparse-matrix sparse-array

我发现数据帧的稀疏版本在保存到磁盘时实际上比密集版本大得多。我做错了什么?

test = pd.DataFrame(ones((4,4000)))
test.ix[:,:] = nan
test.ix[0,0] = 47

test.to_hdf('test3', 'df')
test.to_sparse(fill_value=nan).to_hdf('test4', 'df')

test.to_pickle('test5')
test.to_sparse(fill_value=nan).to_pickle('test6')

....
ls -sh test*
200K test3   16M test4  164K test5  516K test6

使用版本0.12.0

我最终希望有效地存储10 ^ 7个60个阵列,密度大约为10%,然后将它们拉入Pandas数据帧并与它们一起玩。


编辑:感谢Jeff回答原始问题。后续问题:这似乎只能节省酸洗费用,而不是使用其他格式如HDF5。酸洗我最好的路线?

print shape(array_activity) #This is just 0s and 1s
(1020000, 60)

test = pd.DataFrame(array_activity)
test_sparse = test.to_sparse()
print test_sparse.density
0.0832333496732

test.to_hdf('1', 'df')
test_sparse.to_hdf('2', 'df')
test.to_pickle('3')
test_sparse.to_pickle('4')
!ls -sh 1 2 3 4
477M 1  544M 2  477M 3   83M 4

这是一个数据,作为Matlab .mat文件中的索引列表,小于12M。我很想把它变成HDF5 / Pytables格式,这样我就可以抓住特定的索引(其他文件要大得多,加载到内存中需要更长的时间),然后随便做一些Pandasy的事情。也许我不会以正确的方式解决这个问题?

1 个答案:

答案 0 :(得分:6)

你正在创建一个有4000列,只有4行的框架;稀疏是按行处理的,因此反转尺寸。

In [2]: from numpy import *

In [3]: test = pd.DataFrame(ones((4000,4)))

In [4]: test.ix[:,:] = nan

In [5]: test.ix[0,0] = 47

In [6]: test.to_hdf('test3', 'df')

In [7]: test.to_sparse(fill_value=nan).to_hdf('test4', 'df')

In [8]: test.to_pickle('test5')

In [9]: test.to_sparse(fill_value=nan).to_pickle('test6')

In [11]: !ls -sh test3 test4 test5 test6
164K test3  148K test4  160K test5   36K test6

跟进。您提供的商店以table格式编写,因此保存了密集版本(表格格式不支持稀疏,非常灵活且可查询,请参阅docs

此外,您可能希望尝试使用稀疏格式的两种不同表示来保存文件。

所以,这是一个示例会话:

df = 
In [1]: df = pd.read_hdf('store_compressed.h5','test')

In [2]: type(df)
Out[2]: pandas.core.frame.DataFrame

In [3]: df.to_sparse(kind='block').to_hdf('test_block.h5','test',mode='w',complib='blosc',complevel=9)

In [4]: df.to_sparse(kind='integer').to_hdf('test_block.h5','test',mode='w',complib='blosc',complevel=9)

In [5]: df.to_sparse(kind='block').to_hdf('test_block.h5','test',mode='w',complib='blosc',complevel=9)

In [6]: df.to_sparse(kind='integer').to_hdf('test_integer.h5','test',mode='w',complib='blosc',complevel=9)

In [7]: df.to_hdf('test_dense_fixed.h5','test',mode='w',complib='blosc',complevel=9)

In [8]: df.to_hdf('test_dense_table.h5','test',mode='w',format='table',complib='blosc',complevel=9)

In [9]: !ls -ltr *.h5
-rwxrwxr-x 1 jreback users 57015522 Feb  6 18:19 store_compressed.h5
-rw-rw-r-- 1 jreback users 30335044 Feb  6 19:01 test_block.h5
-rw-rw-r-- 1 jreback users 28547220 Feb  6 19:02 test_integer.h5
-rw-rw-r-- 1 jreback users 44540381 Feb  6 19:02 test_dense_fixed.h5
-rw-rw-r-- 1 jreback users 57744418 Feb  6 19:03 test_dense_table.h5
IIRC他们是0.12中的一个错误,因为to_hdf没有通过所有参数,所以你想要使用:

with get_store('test.h5',mode='w',complib='blosc',complevel=9) as store:
    store.put('test',df)

这些基本上作为SparseSeries的集合存储,因此如果密度低且不连续,那么它将不会像大小那样最小。虽然是YMMV,但是Pandas稀疏套件可以更好地处理较少数量的连续块。 scipy也提供了一些稀疏的处理工具。

虽然恕我直言,无论如何这些都是HDF5文件非常简单的大小,你可以处理巨大的行数;并且文件大小分为10和100千兆字节,可以轻松处理(尽管建议)。

此外,如果您确实是查询表,则可以考虑使用表格格式。