atomsmasher atomsmasher - 5 months ago 126
Python Question

pandas, store multiple datasets in an h5 file with pd.to_hdf

Say I have two dataframes,

import pandas as pd
df1 = pd.DataFrame({'col1':[0,2,3,2],'col2':[1,0,0,1]})
df2 = pd.DataFrame({'col12':[0,1,2,1],'col22':[1,1,1,1]})


Now
df1.to_hdf('nameoffile.h5', 'key_to_store','w',table=True)

successully stores
df1
but I want to also store
df2
to the same file, but If I try the same method then
df1
will just be over written. When I try to load it and I check the keys I only see the info of
df2
. How can I store both
df1
and
df2
in the same h5 file as a table ?

Answer

You are using 'w' which overwrites, by default the mode is 'a' so you can just do:

df2.to_hdf('nameoffile.h5', 'key_to_store'',table=True)

Check the docs: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_hdf.html#pandas.DataFrame.to_hdf