简体   繁体   中英

python passing variable name to function

I have various pd.DataFrame s 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)

You should do it like np.savez() does it:

def save_to_hdf(filename, **kwargs):
    with pd.HDFStore(filename) as store:
        for name, df in kwargs.items():
            store.put(name, df)

save_to_hdf('test.h5', df1=df1, another_name=df2)

This is more efficient: it only needs to open the file once to write as many arrays as you want. And you can use names that are different to the variables.

You can avoid having to name the variables twice by using a dict:

dfs = {
    'iris': pd.DataFrame(datasets.load_iris().data),
    'boston': pd.DataFrame(datasets.load_boston().data),
}
save_to_hdf('test.h5', **dfs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM