我已经将CSV文件的列表转换为单独的数据帧,每个列表都从字符串中生成唯一名称,即在伪代码中:df1 =来自CSV1的数据框架,df2 = dataCSV2的框架。它可以正常工作。但是,我怀疑我命名数据帧的方法不是全球范围的正确使用()。是否有更好的方法将字符串分配到数据框架变量?
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)
是否有任何理由将它们全部保存为全局变量?再次恢复它们似乎是真正的痛苦。
我会更改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)
然后,您将获得DF_LIST [0]。