python - 如何根据行值合并两个大小不等的DataFrame

标签 python pandas dataframe

我有两个 pandas 类型的 DataFrame

DataFrame 1

Index   Name    Property1 Property2
0     ("a","b")  1          2
1     ("c","d")  3          4
2     ("e","f")  5          6

第二个,有共同的值(value),但不在同一个索引(我不关心)。

DataFrame 2

Index   Name    Property3 Property4
0     ("g","h")  7          8
1     ("i","j")  9          10
2     ("k","l")  11         12
3     ("a","b")  13         14
4     ("c","d")  15         16
5     ("e","f")  17         18

有没有办法将它们组合起来,使得生成的 DataFrame 是表之间共享名称的公共(public)行?

即 Pandas 操作的结果应该是

Result Frame

Index    Name    Property1 Property2 Property3 Property4
0      ("a","b")  1         2           13       14   
1      ("c","d")  3         4           15       16
2      ("e","f")  5         6           17       18

抱歉,我没有给你实际的 pandas 代码来创建上面的 DataFrame。但我想从概念上理解如何根据列名连接两个具有不同“索引”的大小不等的数据帧。我尝试了合并、连接和加入,但没有得到我想要的结果。

最佳答案

默认merge在这里工作正常,假设你的索引实际上是你的索引:

In [22]:

df1.merge(df2)
Out[22]:
        Name  Property1  Property2  Property3  Property4
0  ("a","b")          1          2         13         14
1  ("c","d")          3          4         15         16
2  ("e","f")          5          6         17         18

此处的merge 查找公共(public)列并对这些列执行内部 合并。

您可以明确指定要在“名称”列上合并:

df1.merge(df2, on='Name')

但在这种情况下没有必要,因为无论如何唯一的公共(public)列是“姓名”。

关于python - 如何根据行值合并两个大小不等的DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30926214/

相关文章:

python - 如何插入字段中带有字符[']的表(pymssql)

Python Pandas <...上的pandas.core.groupby.DataFrameGroupBy对象>

python - pandas MultiIndex 与稀疏矩阵之间的映射

dataframe - Pyspark 收集列表

python - 对矩阵使用降维

python - 使用 NumPy 数组索引列表时出现 TypeError : only integer scalar arrays can be converted to a scalar index

python - 如何使用每小时值和日期时间索引将宽格式转换为长格式?

python - 比较两个连续行并根据特定逻辑运算创建新列

python - 将数据框列字符串值转换为虚拟变量列

python - 如何对连续出现的 pandas 列值的索引进行分组