Home > Enterprise >  merge pandas dataframe from two columns
merge pandas dataframe from two columns

Time:01-05

I have two dataframes with a composite primary key, that is, two columns identify each element, and I would like to merge these dataframes into one. How can I do that ? My example is:

import random
import pandas as pd
import numpy as np
A = ['DF-PI-05', 'DF-PI-09', 'DF-PI-10', 'DF-PI-15', 'DF-PI-16',
       'DF-PI-19']


Sig = [100,200,400]*6
B = np.repeat(A,3)
C = pd.DataFrame(list(zip(B,Sig)),columns=['Name','Sig'])
D = pd.DataFrame(list(zip(B,Sig)),columns=['Name','Sig'])
C['param_1'] = np.linspace(0,20,18)
D['param_2'] = np.linspace(20,100,18)

And I want to merge C and D, by the columns 'Name' and 'Sig', in reality, the column 'Name' is not ordered so, I can not only concatenate or append C to D. I need to match by the two columns together: 'Name' and 'Sig'.

CodePudding user response:

For a simple left merge use:

C.merge(D, on=['Name', 'Sig'], how='left')

To append D to C as rows:

C.append(D.rename(columns={'param_2': 'param_1'}))
  • Related