2016-11-11 71 views
2

熊猫数据框通常以long(很多行)或wide(很多列)格式表示。将数据保存在熊猫中时宽vs长格式hdf5

我不知道哪种格式更快地读取并占据时保存为HDF文件(df.to_hdf)的内存更少。

是否有一个通用规则或某些格式应该是首选的情况?

+0

,你打算用什么样的dtypes的?对于你如何存储你的DF或者你是否要转置他们真的不重要? – MaxU

+0

我有不同的数据框。有些人只有漂浮而其他人有绳子和漂浮物。他们安静的大(100GB),我想,以减少内存使用和阅读时间尽可能。 – Donbeo

回答

0

IMO长格式更加可取,因为您的元数据开销要小得多(有关列名,dtypes等的信息)。

在内存使用的术语,他们会或多或少相同:

In [22]: long = pd.DataFrame(np.random.randint(0, 10**6, (10**4, 4))) 

In [23]: wide = pd.DataFrame(np.random.randint(0, 10**6, (4, 10**4))) 

In [24]: long.shape 
Out[24]: (10000, 4) 

In [25]: wide.shape 
Out[25]: (4, 10000) 

In [26]: sys.getsizeof(long) 
Out[26]: 160104 

In [27]: sys.getsizeof(wide) 
Out[27]: 160104 

In [28]: wide.info() 
<class 'pandas.core.frame.DataFrame'> 
RangeIndex: 4 entries, 0 to 3 
Columns: 10000 entries, 0 to 9999 
dtypes: int32(10000) 
memory usage: 156.3 KB 

In [29]: long.info() 
<class 'pandas.core.frame.DataFrame'> 
RangeIndex: 10000 entries, 0 to 9999 
Data columns (total 4 columns): 
0 10000 non-null int32 
1 10000 non-null int32 
2 10000 non-null int32 
3 10000 non-null int32 
dtypes: int32(4) 
memory usage: 156.3 KB