【发布时间】:2018-09-29 11:53:44
【问题描述】:
我有两个数据框:
import pandas as pd
# Column contains column name
df1 = pd.DataFrame({"Column": pd.Series(['a', 'b', 'b', 'c']),
"Item": pd.Series(['x', 'y', 'z', 'x']),
"Result": pd.Series([3, 4, 5, 6])})
df2 = pd.DataFrame({"a": pd.Series(['x', 'n', 'n']),
"b": pd.Series(['x', 'y', 'n']),
"c": pd.Series(['x', 'z', 'n'])})
如何根据“列”中的“项目”将“结果”添加到 df2? 预期的数据帧 df2 是:
a b c Result
- - - ------
x x x 3
n y z 4
n n n null
上述问题怎么可能是 3 个问题的重复,其中 2 个问题被 @smci 标记为“或”?
【问题讨论】:
-
这称为“合并”或“加入”。特别是 df1 的
Result列的 df2 上的左连接。你正在尝试加入df1.Column==df2.index -
@iDrwish 请重新发表您的评论并编辑问题标题和文本的说明。说明为什么这不是一个重复的问题。
-
所以实际上这是Pandas merge using dfA column == dfB index 或Pandas merge on index column 或类似的副本。我现在不能改变我的投票,但请投票给那些。 (唯一的细微差别是 OP 只想合并
Result列,但这很简单。) -
@smci 你能给出这个合并/加入的命令吗?
标签: pandas join merge left-join