[英]wide vs long format when saving data in pandas hdf5
大熊貓數據框通常以長(很多行)或寬(很多列)格式表示。
我想知道哪種格式保存為hdf文件( df.to_hdf
)時讀取速度更快,占用的內存更少。
是否有一般規則或某些情況下應首選一種格式?
最好使用IMO長格式,因為元數據開銷(有關列名,dtype等的信息)要少得多。
就內存使用而言,它們將大致相同:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.