Reading and writing Pandas DataFrames to HDF5 stores
The HDFStore
class is the pandas abstraction responsible for dealing with HDF5 data. Using random data, we will demonstrate this functionality.
Give the HDFStore
constructor the path to a demo file and create a store:
filename = "pytable_df_demo.h5" store = pd.io.pytables.HDFStore(filename) print(store)
The preceding code snippet will print the file path to the store and its contents, which is empty at the moment:
<class 'pandas.io.pytables.HDFStore'> File path: pytable_df_demo.h5 Empty
HDFStore
has a dict-like interface, meaning that we can store values, such as, for instance, a pandas DataFrame with a corresponding lookup key. Store a DataFrame containing random data in HDFStore
as follows:
store['df'] = df print(store)
Now the store contains data as illustrated in the following output:
<class 'pandas.io.pytables.HDFStore'> File path: pytable_df_demo.h5 ...