I need help on vlookup using python. I need only one matching column data of df2 in df1 instead of all df2 data. my input is below two dataframes.
df1
NodeName NEID Configured_Speed
MUM 25234 511.054
DEL 32251 154.155
CHN 32584 224.949
KOL 27076 372.932
PUN 29743 203.556
TN 29037 224.949
df2
NodeName Address Region
KOL 10.134.9.242 East
DEL 10.51.195.236 North
CHN 10.139.56.59 South
TN 10.133.19.251 South
KEL 10.51.60.6 South
MUM 10.1.59.193 West
PUN 10.51.60.66 West
and my Output is
NodeName NEID Configured_Speed Address
MUM 25234 511.054 10.1.59.193
DEL 32251 154.155 10.51.195.236
CHN 32584 224.949 10.139.56.59
KOL 27076 372.932 10.134.9.242
PUN 29743 203.556 10.51.60.66
TN 29037 224.949 10.133.19.251
I tried below code but getting Address column blank.
df1['Address']=df1.NodeName.map(df2.Address)
and when i use below code, all columns from df2
came in df1
df3 = pd.merge(df1, df2, on ='NodeName',how ='left')
CodePudding user response:
You can subset df2
to send only the desired columns for the merging process-
df3 = pd.merge(df1, df2[['NodeName', 'Address']], on ='NodeName', how ='left')
CodePudding user response:
You can try
df3 = pd.merge(df1, df2['NodeName','Address'],on='NodeName',how='left')