I have various pd.DataFrames that I'd like to write to an hdf store by passing them to a function. Is there a way to programmatically generate key names based on the variable name of any given dataframe?
from sklearn import datasets
import pandas as pd
df1 = pd.DataFrame(datasets.load_iris().data)
df2 = pd.DataFrame(datasets.load_boston().data)
def save_to_hdf(df1):
with pd.HDFStore('test.h5') as store:
store.put('df1', df1)
save_to_hdf(df1)
df1, both the name, and the actual DataFrame? It's a tiny bit more work, but it makes thing much clearer. Or use a dict, like{'df1': df1, 'df2': df2}, and iterate over the items. It's also more flexible.globals()['df1']to get the relevant DataFrame, but I wouldn't recommend it.