Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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 ?

like image 506
atomsmasher Avatar asked Mar 12 '23 18:03

atomsmasher


1 Answers

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, mode='a')

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

like image 91
EdChum Avatar answered Apr 28 '23 04:04

EdChum