Loading [MathJax]/extensions/tex2jax.js

News

New paper!

Tuesday, December 12, 2023

Handling hdf files in python with pandas

import pandas as pd
import numpy
df1 = pd.DataFrame(np.random.rand(100,5), columns=list('abcde')) # example data table with 5 columns
df2 = pd.DataFrame(np.random.rand(100,5), columns=list('abcde')) # example data table with 5 columns
filename = "data.hdf5"
df1.to_hdf(filename,"X", mode="w") # "X" is the key to access df1. mode="w" creates a new file.
df2.to_hdf(filename,"Y",format="table",data_columns=True, mode="a", index=False)
# table format allows to access by columns. mode="a" is to append.
data1 = pd.read_hdf(filename,"X")
data2 = pd.read_hdf(filename,"Y",columns=["a","c"]) # reading the table df2 of columns a and c
view raw example_hdf5.py hosted with ❤ by GitHub