Search code examples
pythondataframeconcatenation

Concatenating two DataFrames but only for common values in Python


I have 2 dataframes like this:

name    code phone_number 
Joe     BX13    03453
Bill    C308    321356
Donald  H314    34532
Mike    J4D6    2134

code vehicle
C308 Mercedes
H314 BMW

I would like to concatenate them but only for common values in a specific column so it would look like this:

name    code vehicle
Bill    C308 Mercedes
Donald  H314   BMW

df3=pd.concat([df1, df2])

looks promising but I don't know to specify that I only want the common values. What arguments should I put in the parenthesis?


Solution

  • You can use df.merge

    Setting up your data

    import pandas as pd
    import io
    t = '''
    name,code,phone_number
    Joe,BX13,3453
    Bill,C308,321356
    Donald,H314,34532
    Mike,J4D6,2134'''
    df1 = pd.read_csv(io.StringIO(t))
    
    t = '''
    code,vehicle
    C308,Mercedes
    H314,BMW'''
    df2 = pd.read_csv(io.StringIO(t))
    

    Now you can merge the two dataframes with selected columns

    df1[['name','code']].merge(df2, on='code', how='inner')
    

    Out:

         name  code   vehicle
    0    Bill  C308  Mercedes
    1  Donald  H314       BMW