我有两个 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/