Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading