Book Image

Python Data Analysis

By : Ivan Idris
Book Image

Python Data Analysis

By: Ivan Idris

Overview of this book

Table of Contents (22 chapters)
Python Data Analysis
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Key Concepts
Online Resources
Index

Reading and writing pandas DataFrames to HDF5 stores


The HDFStore class is the pandas abstraction responsible for dealing with HDF5 data. Using random data and temporary files, we will demonstrate this functionality. These are the steps to do so:

Give the HDFStore constructor the path to a temporary file and create a store:

store = pd.io.pytables.HDFStore(tmpf.name)
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: /var/folders/k_/xx_xz6xj0hx627654s3vld440000gn/T/tmpfmwPPB
Empty

HDFStore has a dict-like interface, meaning that we can store values, 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: /var/folders/k_/xx_xz6xj0hx627654s3vld440000gn...