Home > OS >  concat dataframes from two dictionaries that contain data frames
concat dataframes from two dictionaries that contain data frames

Time:04-07

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

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]])
updated_dict_df=dict(zip(keysList,updated_df_map))

CodePudding user response:

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)
  • Related