Dictionary data merge

Say I have a data dictionary:

{'df1': name color type Apple Yellow Fruit, 'df2': name color type Banana Red Fruit, 'df3': name color type Chocolate Brown Sweet ......} 

And I want to combine them all into one:

  name color type Apple Red Fruit Banana Yellow Fruit Chocolate Brown Sweet 

I can do it manually as follows:

  merge1=pd.merge('df1','df2') merge2=pd.merge('merge1','df3') ... 

But is there a way to automatically encrypt the dictionary and merge? Any help is appreciated.

+6
source share
1 answer

You can simply pass the dict direct and access the values attribute in concat :

 In [233]: d Out[233]: {'df1': name color type 0 Apple Yellow Fruit, 'df2': name color type 0 Banana Red Fruit, 'df3': name color type 0 Chocolate Brown Sweet} In [234]: pd.concat(d.values(), ignore_index=True) Out[234]: name color type 0 Banana Red Fruit 1 Apple Yellow Fruit 2 Chocolate Brown Sweet 

This assumes that you just want to merge all the dfs, if you are going to merge, then you need to explain what merge criteria

+10
source

Source: https://habr.com/ru/post/987192/


All Articles