yasin mohammed
yasin mohammed

Reputation: 471

Mapping data from 2 dataframes in pandas

I have two dataframes which I need to map and final output should look like this.

Input 1

LPAR    DC
A       LA
B       NY
C       CT
D       VA
E       WO

Input 2

LPAR    PROD
A       Microsoft
A       Symantec
A       Vmware
B       Compuware
C       BMC
B       CA

Final Output

LPAR    DC  PROD
A       LA  Microsoft
A       LA  Symantec
A       LA  Vmware
B       NY  Compuware
B       NY  CA
C       CT  BMC
D       VA  
E       WO  

Upvotes: 0

Views: 2726

Answers (1)

ArunDhaJ
ArunDhaJ

Reputation: 631

You can merge two DataFrames and fillna with empty string

dict1 = {'LPAR': ['A', 'B', 'C', 'D', 'E'], 
            'DC': ['LA', 'NY', 'CT', 'VA', 'WO']}
df1 = pd.DataFrame(dict1)

dict2 = {'LPAR': ['A', 'A', 'A', 'B', 'B', 'C'], 
             'PROD': ['Microsoft', 'Symantec', 'Vmware', 'Compuware', 'CA', 'BMC']}
df2 = pd.DataFrame(dict2)

df3 = df1.merge(df2, on='LPAR', how='outer').fillna('')

Upvotes: 2

Related Questions