concat dataframes from two dictionaries that contain data frames

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

The keys are identical and in the same order in both dictionaries.
I want to concat the dataframes and end up with a new dictionary where the keys have the value of the combined data frames.

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

Seems pretty easy, but I’m having issues using pd.concat and setting one of the default arguments (ignore_index=true). Below is the code I tried that doesn’t seem to work, although I think it should, lol:

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

>Solution :

You could use zip in a dict comprehension to traverse the values of the two dictionaries together and use concat to concatenate as you iterate (since the keys match, we only need to iterate over 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())}

If updating dict_df suffices, you could use a loop:

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

Leave a ReplyCancel reply