from pandas import (
DataFrame, HDFStore
)
import pandas as pd
import numpy as np
df = DataFrame(np.random.randn(5,3), columns=['A', 'B', 'C',])
df
A | B | C | |
---|---|---|---|
0 | -0.092894 | 0.480401 | -0.967241 |
1 | -1.003829 | 0.012645 | 0.527720 |
2 | -0.060884 | -0.088839 | -0.269744 |
3 | 0.729817 | -0.042234 | 0.229410 |
4 | -1.117705 | -0.778368 | -1.280790 |
store = HDFStore('dataset.h5')
store
<class 'pandas.io.pytables.HDFStore'> File path: dataset.h5 Empty
store.put('d1', df, format='table', data_columns=True)
store
<class 'pandas.io.pytables.HDFStore'> File path: dataset.h5 /d1 frame_table (typ->appendable,nrows->5,ncols->3,indexers->[index],dc->[A,B,C])
store['d1']
A | B | C | |
---|---|---|---|
0 | -0.092894 | 0.480401 | -0.967241 |
1 | -1.003829 | 0.012645 | 0.527720 |
2 | -0.060884 | -0.088839 | -0.269744 |
3 | 0.729817 | -0.042234 | 0.229410 |
4 | -1.117705 | -0.778368 | -1.280790 |
store.append('d1', DataFrame(np.random.randn(5,3), columns=['A', 'B', 'C']))
store
<class 'pandas.io.pytables.HDFStore'> File path: dataset.h5 /d1 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[A,B,C])
store['d1']
A | B | C | |
---|---|---|---|
0 | -0.092894 | 0.480401 | -0.967241 |
1 | -1.003829 | 0.012645 | 0.527720 |
2 | -0.060884 | -0.088839 | -0.269744 |
3 | 0.729817 | -0.042234 | 0.229410 |
4 | -1.117705 | -0.778368 | -1.280790 |
0 | -0.396292 | 0.205355 | 0.995982 |
1 | 0.450495 | -0.744076 | -1.320831 |
2 | 1.003412 | -0.876143 | 1.677286 |
3 | -0.395701 | -0.465095 | 0.287003 |
4 | 1.442614 | 0.818815 | -0.378552 |
store.close()
store
<class 'pandas.io.pytables.HDFStore'> File path: dataset.h5 File is CLOSED
df = pd.read_hdf('dataset.h5')
df
A | B | C | |
---|---|---|---|
0 | -0.092894 | 0.480401 | -0.967241 |
1 | -1.003829 | 0.012645 | 0.527720 |
2 | -0.060884 | -0.088839 | -0.269744 |
3 | 0.729817 | -0.042234 | 0.229410 |
4 | -1.117705 | -0.778368 | -1.280790 |
0 | -0.396292 | 0.205355 | 0.995982 |
1 | 0.450495 | -0.744076 | -1.320831 |
2 | 1.003412 | -0.876143 | 1.677286 |
3 | -0.395701 | -0.465095 | 0.287003 |
4 | 1.442614 | 0.818815 | -0.378552 |
store = HDFStore('dataset.h5')
store
<class 'pandas.io.pytables.HDFStore'> File path: dataset.h5 /d1 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[A,B,C])
store.put('d2', DataFrame(np.random.randn(7,4)))
store.put('d3', DataFrame(np.random.randn(14,3)))
store
<class 'pandas.io.pytables.HDFStore'> File path: dataset.h5 /d1 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[A,B,C]) /d2 frame (shape->[7,4]) /d3 frame (shape->[14,3])
store.append('d1', pd.DataFrame(np.random.randn(3,3), columns=['A', 'B', 'C']))
store['d1']
A | B | C | |
---|---|---|---|
0 | -0.092894 | 0.480401 | -0.967241 |
1 | -1.003829 | 0.012645 | 0.527720 |
2 | -0.060884 | -0.088839 | -0.269744 |
3 | 0.729817 | -0.042234 | 0.229410 |
4 | -1.117705 | -0.778368 | -1.280790 |
0 | -0.396292 | 0.205355 | 0.995982 |
1 | 0.450495 | -0.744076 | -1.320831 |
2 | 1.003412 | -0.876143 | 1.677286 |
3 | -0.395701 | -0.465095 | 0.287003 |
4 | 1.442614 | 0.818815 | -0.378552 |
0 | -0.196617 | 0.585134 | 1.052465 |
1 | 0.386223 | -0.932965 | 0.749832 |
2 | 0.721093 | 1.453562 | -0.426798 |
store.append('d2', pd.DataFrame(np.random.randn(4,4)))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-22-966a756e5b6e> in <module>() ----> 1 store.append('d2', pd.DataFrame(np.random.randn(4,4))) C:\Users\Jeri_Dabba\pandas\io\pytables.pyc in append(self, key, value, format, append, columns, dropna, **kwargs) 917 kwargs = self._validate_format(format, kwargs) 918 self._write_to_group(key, value, append=append, dropna=dropna, --> 919 **kwargs) 920 921 def append_to_multiple(self, d, value, selector, data_columns=None, C:\Users\Jeri_Dabba\pandas\io\pytables.pyc in _write_to_group(self, key, value, format, index, append, complib, encoding, **kwargs) 1250 if (not s.is_table or 1251 (s.is_table and format == 'fixed' and s.is_exists)): -> 1252 raise ValueError('Can only append to Tables') 1253 if not s.is_exists: 1254 s.set_object_info() ValueError: Can only append to Tables
store['d1/table']
index | A | B | C | |
---|---|---|---|---|
0 | 0 | -0.092894 | 0.480401 | -0.967241 |
1 | 1 | -1.003829 | 0.012645 | 0.527720 |
2 | 2 | -0.060884 | -0.088839 | -0.269744 |
3 | 3 | 0.729817 | -0.042234 | 0.229410 |
4 | 4 | -1.117705 | -0.778368 | -1.280790 |
5 | 0 | -0.396292 | 0.205355 | 0.995982 |
6 | 1 | 0.450495 | -0.744076 | -1.320831 |
7 | 2 | 1.003412 | -0.876143 | 1.677286 |
8 | 3 | -0.395701 | -0.465095 | 0.287003 |
9 | 4 | 1.442614 | 0.818815 | -0.378552 |
10 | 0 | -0.196617 | 0.585134 | 1.052465 |
11 | 1 | 0.386223 | -0.932965 | 0.749832 |
12 | 2 | 0.721093 | 1.453562 | -0.426798 |
df = store['d1']
df
A | B | C | |
---|---|---|---|
0 | -0.092894 | 0.480401 | -0.967241 |
1 | -1.003829 | 0.012645 | 0.527720 |
2 | -0.060884 | -0.088839 | -0.269744 |
3 | 0.729817 | -0.042234 | 0.229410 |
4 | -1.117705 | -0.778368 | -1.280790 |
0 | -0.396292 | 0.205355 | 0.995982 |
1 | 0.450495 | -0.744076 | -1.320831 |
2 | 1.003412 | -0.876143 | 1.677286 |
3 | -0.395701 | -0.465095 | 0.287003 |
4 | 1.442614 | 0.818815 | -0.378552 |
0 | -0.196617 | 0.585134 | 1.052465 |
1 | 0.386223 | -0.932965 | 0.749832 |
2 | 0.721093 | 1.453562 | -0.426798 |
store.close()