从不同词典合并多个Pandas DataFrames(DF)的最优雅方式是什么?
我有两个字典用于创建几个不同的DF。我用来从每个键内的列表创建DF的第一个字典。我用来概述DF的特征的第二个字典,用于不同文本文件的read_csv方法。所有DF都有一个名为“FN'”的公共列。所以我希望在两个词典之间合并DataFrame的所有组合。
Dict1={List1:{'List_data':[some_data],
'Match_txt_ID':'Textfilename'},
List2:{'List_data':[some_data],
'Match_txt_ID':'Textfilename'},
}
Dict2={Filetype1:{'CSV_Endswith:'part_of_filename1',
'Other_data':'Some_data'},
Filetype2:{'CSV_Endswith':'part_of_filename2',
'Other_data':'Some_data'},
}
所以最终使用Pandas我希望使用词典Dict1和Dict2合并以下组合来帮助。
List1+Filetype1
List2+Filetype1
List1+Filetype2
List2+Filetype2
同时所有这些都将在循环中完成。所以我会合并List1 + Filetype1并执行一些统计数据收集(然后将统计数据发布到Excel),然后发布List2 + Filetype1并执行更多的统计数据收集(然后将统计数据发布到Excel)。
我有下面的组成部分,但我不知道如何通过加入以下三个步骤来有效地循环。
for csvfile in os.listdir(some_directory):
if csvfile.endswith('.txt'):
for key in Dict2:
if Dict2[key]['CSV_Endswith'] in csvfile:
csv2df = pd.read_csv(csvfile,sep=';')
Dict2_DF = DataFrame(csv2df)
for key in Dict1:
Dict1_DF = DataFrame(Dict1[key]['List_data'])
MERGED_DF = pd.merge(Dict1_DF, Dict2_DF, on=['FN'], how='outer')
#do stuff with MERGED_DF
由于