我想比较两个数据帧df1和df2中的名称列,输出数据帧df1中的匹配行,并将结果存储在新数据帧df3中。我如何在熊猫中做到这一点?
DF1
place name qty unit
NY Tom 2 10
TK Ron 3 15
Lon Don 5 90
Hk Sam 4 49
df2
place name price
PH Tom 7
TK Ron 5
结果:
DF3
place name qty unit
NY Tom 2 10
TK Ron 3 15
答案 0 :(得分:0)
选项1
使用BEGIN
WHILE (qqty > 0) DO
IF (maintainStock = 1) THEN
SET @date1 = (SELECT MIN(expiry_date) FROM `stock_current` WHERE closing > 0);
UPDATE `stock_current` SET sold = sold + 1, closing = closing - 1 WHERE item_id = iid AND expiry_date = @date1;
SET qqty = qqty - 1;
ELSE
SET @date2 = (SELECT MIN(expiry_date) FROM `stock_current`);
UPDATE `stock_current` SET sold = sold + 1 WHERE item_id = iid AND expiry_date = @date2;
SET qqty = qqty - 1;
END IF;
END WHILE;
END
:
df.isin
选项2
使用In [1362]: df1[df1.name.isin(df2.name)]
Out[1362]:
place name qty unit
0 NY Tom 2 10
1 TK Ron 3 15
执行内部联接:
df.merge
选项3
使用In [1365]: df1.merge(df2.name.to_frame())
Out[1365]:
place name qty unit
0 NY Tom 2 10
1 TK Ron 3 15
:
df.eq
答案 1 :(得分:0)
你想要一种叫做内连接的东西。
df1.merge(df2,on = 'name')
place_x name qty unit place_y price
NY Tom 2 10 PH 7
TK Ron 3 15 TK 5
当两个数据框中的列合并时,会发生_x
和_y
。