Pandas: Adding a new column to a dataset based on a condition using information from another dataset

Question:

I have 2 csv files. One dataset, df1, with a single column that looks like this

   deviceNames
0     12132182
1     12134086
2     12203676
3     12131211
4     12129534

And another, df2, with many columns but the relevant columns are

`       deviceNames        macAddress
0         12080084  001350050039517e
1         12080085  001350050039448c
2         12080086  00135005003954c9
3         12080087  00135005003943bc
4         12080088  0013500500394ff5
...            ...               ...
107549    C0524751  0013500500EA4DEB
107550         NaN               NaN
107551         NaN               NaN
107552         NaN               NaN
107553    C0591266  00135005010FB39D`

What I want is to bring over the mac address information from df2 to df1 based on the device names in df1

So I want the output to look like this


       deviceNames        macAddress
0         12132182  0013500124039517e
1         12134086  0013501340039448c
2         12203676  001350440031954c9
3         12131211  0013503300w3943bc
4         12129534  00135032500394ff5

My attempts

  • Here are a few attempts I made at trying to solve this issue

1

df2[df2['deviceNames'].isin(df1['deviceNames'])]

2

 s = (df2.loc[df2.deviceNames.isin(df1.deviceNames.values.tolist())]
     .drop_duplicates('df1')
     .set_index('df1')['macAddress'])

df1['newcolumn'] = df1['newcolumn'].map(s)
Asked By: jondidathing

||

Answers:

What you are looking for is merge but your expected output doesn’t match your two input dataframes so maybe I’m wrong:

out = df1.astype({'deviceNames': str}).merge(df2, on='deviceNames', how='left')
Answered By: Corralien
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.