Pandas 有没有一种方法可以合并熊猫中的2个数据帧,从而保持df1的值不变
尝试合并panda“表A”和“表”中的两个数据帧 “表A”有200K行,表B有310K行。一旦合并,我想要 “表A”的行保持200K。我尝试使用左、右、外 合并后,由于某种原因,表A的行不会保持在200K`Pandas 有没有一种方法可以合并熊猫中的2个数据帧,从而保持df1的值不变,pandas,merge,concatenation,pandasql,Pandas,Merge,Concatenation,Pandasql,尝试合并panda“表A”和“表”中的两个数据帧 “表A”有200K行,表B有310K行。一旦合并,我想要 “表A”的行保持200K。我尝试使用左、右、外 合并后,由于某种原因,表A的行不会保持在200K` This is the table that I have and I'm trying to merge table A ID pass date Suitcase Layover 500
This is the table that I have and I'm trying to merge
table A
ID pass date Suitcase Layover
500 yes 2/22/2018 1 yes
501 no 2/23/2018 3 yes
502 yes 2/24/2018 5 yes
504 yes 2/25/2018 2 no
505 yes 2/26/2018 1 yes
506 no 2/27/2018 2 no
507 yes 2/28/2018 5 no
560 yes 3/15/2019 2 yes
Table B
ID time_travel country
500 4 USA
504 3 MEXICO
507 1 Canada
621 2 Australia
3345 3 South Africa
7755 2 France
3385 1 California
我尝试了所示的示例,但它返回的值较少,当表B中存在重复的
ID
时,这是weirdit的正常现象。要使用LEFT JOIN
在表A中保留#个记录,您可以使用drop\u duplicates
或groupby()基于ID删除表B中的重复数据
。我尝试了所示的示例,但它返回的值较少,这在表B中存在重复的ID
时是weirdit的正常现象。要使用左连接保留表A中的记录,可以使用删除重复项或groupby()
基于ID删除表B中的重复项。
merging2 = pd.merge(table A,Table B, on=["id"],
how="left",indicator=True)
merging2.head()
``` the goal is to have the column ID stay at 200k and to have a table that
looks like this
id pass date Suitcase Layover time_travel country
500 yes 2/22/2018 1 yes 4 USA
501 no 2/23/2018 3 yes
502 yes 2/24/2018 5 yes
504 yes 2/25/2018 2 no 3 MEXICO
505 yes 2/26/2018 1 yes
506 no 2/27/2018 2 no
507 yes 2/28/2018 5 no Canada