合并字典中的数据帧

qts*_*qts 12 python pandas

假设我有一个数据帧字典:

  {'df1': name         color    type
          Apple        Yellow   Fruit,
   'df2': name         color    type
          Banana       Red      Fruit,
   'df3': name         color    type
          Chocolate    Brown    Sweet
    ......}
Run Code Online (Sandbox Code Playgroud)

我想将它们合并为一个像这样:

  name         color    type
  Apple        Red      Fruit 
  Banana       Yellow   Fruit
  Chocolate    Brown    Sweet
Run Code Online (Sandbox Code Playgroud)

我可以手动完成,如下所示:

  merge1=pd.merge('df1','df2')
  merge2=pd.merge('merge1','df3')
  ...
Run Code Online (Sandbox Code Playgroud)

但有没有办法自动压缩字典并合并?任何帮助表示赞赏.

EdC*_*ica 22

您可以直接传递dict并访问该values属性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
Run Code Online (Sandbox Code Playgroud)

这假设您只是想要连接所有dfs,如果要合并,那么您需要解释合并条件是什么