We can also read and write HDF5 files with pandas. To read HDF5 files with pandas, they must first be created with it. For example, let's use pandas to create a HDF5 file containing global power values:
import pandas as pd import numpy as np arr = np.loadtxt('temp.csv', skiprows=1, usecols=(2,3), delimiter=',') import pandas as pd store=pd.HDFStore('hdfstore_demo.hdf5') print(store) store['global_power']=pd.DataFrame(arr) store.close()
Now let's read the HDF5 file that we created and print the array back:
import pandas as pd store=pd.HDFStore('hdfstore_demo.hdf5') print(store) print(store['global_power']) store.close()
The values of the DataFrame can be read in three different ways:
store['global_power']
store.get('global_power')
store.global_power
pandas also provides the high-level read_hdf() function and the to_hdf() DataFrame method for reading and writing HDF5 files.