连接来自两个包含数据框的字典的数据框

concat dataframes from two dictionaries that contain data frames

#dictionary containing values that are dataframes
dict_df
#another dictionary containing values that are dataframes
new_dict_df 

两个词典中的键完全相同且顺序相同。 我想连接数据帧并最终得到一个新字典,其中的键具有组合数据帧的值。

updated_dict_df[key1]= dict_df[key1]+new_dict_df[key1]
updated_dict_df[key2]=dict_df[key2]+new_dict_df[key2] etc etc etc

看起来很简单,但我在使用 pd.concat 和设置默认参数之一 (ignore_index=true) 时遇到问题。下面是我试过的代码,虽然我认为应该行,但似乎行不通,哈哈:

updated_df_map= map(functools.partial(pd.concat,ignore_index=True),[[*new_dict_df],[*df_dict]])
updated_dict_df=dict(zip(keysList,updated_df_map))

你可以在字典推导中使用zip将两个字典的值一起遍历,并在迭代时使用concat连接(因为键匹配,我们只需要迭代new_dict_df.values()):

out = {k: pd.concat([df1, df2], ignore_index=True) for (k, df1), df2 in zip(dict_df.items(), new_dict_df.values())}

如果更新 dict_df 就足够了,您可以使用循环:

for (k, df1), df2 in zip(dict_df.items(), new_dict_df.values()):
    dict_df[k] = pd.concat([df1, df2], ignore_index=True)