我有两个数据框:
df1 = pd.DataFrame({'X1':[2,2,2,2,3,3,3,3,4,4,4,5,5,5,5],
'X2':[10,20,70,80,10,20,70,80,50,70,80,60,70,80,90],
'X3':[0,1,0,1,1,1,1,0,0,0,1,1,0,0,0]})
X1 X2 X3
0 2 10 0
1 2 20 1
2 2 70 0
3 2 80 1
4 3 10 1
5 3 20 1
6 3 70 1
7 3 80 0
8 4 50 0
9 4 70 0
10 4 80 1
11 5 60 1
12 5 70 0
13 5 80 0
14 5 90 0
,另一个是df2:
df2 = pd.DataFrame({'X1':[2,3,3,4,5,5,5],
'X2':[10,20,70,50,60,70,80]})
X1 X2
0 2 10
1 3 20
2 3 70
3 4 50
4 5 60
5 5 70
6 5 80
我需要根据df1每一行中X1和X2的值来找到df2的相应X3值。结果应该类似于以下内容:
X1 X2 X3
0 2 10 0
1 3 20 1
2 3 70 1
3 4 50 0
4 5 60 1
5 5 70 0
6 5 80 0
答案 0 :(得分:2)
使用pandas.DataFrame.merge
:
df3 = df2.merge(df1, on=["X1", "X2"])
print(df3)
输出:
X1 X2 X3
0 2 10 0
1 3 20 1
2 3 70 1
3 4 50 0
4 5 60 1
5 5 70 0
6 5 80 0
答案 1 :(得分:1)
pandas.DataFrame.join
on=
参数提供了多个值,则other
DataFrame必须具有一个MultiIndex,因此df1
上的.set_index
。df2 = df2.join(df1.set_index(['X1', 'X2']), on=['X1', 'X2'])
# display(df2)
X1 X2 X3
0 2 10 0
1 3 20 1
2 3 70 1
3 4 50 0
4 5 60 1
5 5 70 0
6 5 80 0