pandas :如何以以下方式连接数据帧? [英] Pandas: How to concatenate dataframes in the following manner?

查看:69
本文介绍了 pandas :如何以以下方式连接数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多个数据帧连接成一个数据帧。我希望连接发生的方式在以下示例中进行了说明:

I want to concatenate multiple dataframes into one dataframe. The manner I want the concatenation to happen is illustrated in the following example:

Input tables:
   A  B  C  D
0  x  p  2  4
1  y  q  3  5

   A  B  E  F
0  x  p  6  8
1  y  q  9  10

Output table:

   A  B  C  D  E  F
0  x  p  2  4  6  8
1  y  q  3  5  9  10

我想知道是否可以使用pandas.concat命令来完成。我知道可以通过pd.merge命令来完成。

谢谢。

I want to know if this can be done using the pandas.concat command. I know this can be done through the pd.merge command.
Thanks.

推荐答案

使用 set_index ,其中包含 MultiIndex 的列表理解,然后 concat

Use set_index with list comprehension for MultiIndex and then concat:

dfs = [df1, df2, df3]

df = pd.concat([x.set_index(['A','B']) for x in dfs], axis=1)

这篇关于 pandas :如何以以下方式连接数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆