Comparing 2 columns of two Python Pandas dataframes and getting the common rows

前端 未结 4 999
旧巷少年郎
旧巷少年郎 2020-12-03 18:43

I have 2 Dataframe as follows:

DF1=
    A    B   C    D
0   AA   BA  KK   0
1   AD   BD  LL   0
2   AF   BF  MM   0

DF2=
    K    L
0   AA   BA
1   AD   BF
         


        
4条回答
  •  庸人自扰
    2020-12-03 19:43

    DF1.merge(right=DF2,left_on=[DF1.A,DF1.B],right_on=[DF2.K,DF2.L], indicator=True, how='left')

    gives:

    A B C D K L _merge 0 AA BA KK 0 AA BA both 1 AD BD LL 0 NaN NaN left_only 2 AF BF MM 0 AF BF both

    So, as above, indicator does the job.

    Peter

提交回复
热议问题