从字符串列表中为多个数据帧指定名称

时间:2017-11-29 01:56:24

标签: python string python-3.x csv global-variables

我已将csv文件列表转换为单独的数据帧,每个数据帧都具有从字符串生成的唯一名称,即伪代码:df1 =来自csv1的数据帧,df2 =来自csv2的数据帧。它工作正常。但是,我怀疑我的数据帧命名方法不正确使用globals()。有没有更好的方法将字符串分配给数据框变量?

def csv_to_df(csv_list):
'''Takes a list of input csv files as strings. Converts the file names to pandas dataframes.
The data frames are assigned names df1, df2, df3...dfn'''
# make a list of dataframes
df_list = ['df' + str(item) for item in range(1, len(csv_list) + 1)]
for x, y in zip(df_list, csv_list):
    globals()[x] = pd.read_csv(y, header=None,names=['Time', 'Temp'],  delimiter='\t')

files = ['../data/something', '../data/something else',
     '../data/something else here', '../data/something for sure',
     '../data/something sometimes']

csv_to_df(files)

1 个答案:

答案 0 :(得分:1)

你有什么理由想把它们全部保存为全局变量吗?再次恢复它们似乎真的很痛苦。

我改变df_list来代替保存数据帧

def csv_to_df(csv_list):
'''Takes a list of input csv files as strings. Converts the file names to pandas dataframes.
The data frames are assigned names df1, df2, df3...dfn'''
# make a dictionary of dataframes
df_dict = {}
for i, df in enumerate(csv_list):
    df_dict['df' + str(i+1)] = pd.read_csv(y, header=None,names=['Time', 'Temp'],  delimiter='\t')

files = ['../data/something', '../data/something else',
     '../data/something else here', '../data/something for sure',
     '../data/something sometimes']

csv_to_df(files)

然后你得到df_dict [' df1']而不是获得df1。您甚至可以将它们直接放在列表中并按索引获取它们。

def csv_to_df(csv_list):
'''Takes a list of input csv files as strings. Converts the file names to pandas dataframes.
The data frames are assigned names df1, df2, df3...dfn'''
# make a list of dataframes
df_list = []
for df in csv_list:
    df_list.append(pd.read_csv(y, header=None,names=['Time', 'Temp'],  delimiter='\t'))

files = ['../data/something', '../data/something else',
     '../data/something else here', '../data/something for sure',
     '../data/something sometimes']

csv_to_df(files)

然后代替df1,你得到df_list [0]。