Pandas:使用来自另一个数据集的信息根据条件向数据集添加新列

问题描述 投票:0回答:1

我有 2 个 csv 文件。一个数据集 df1,只有一列,看起来像这样

   deviceNames
0     12132182
1     12134086
2     12203676
3     12131211
4     12129534

另一个,df2,有很多列,但相关列是

`       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`

我要的是根据df1中的设备名把df2的mac地址信息传给df1

所以我希望输出看起来像这样


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

我的尝试

  • 以下是我为解决这个问题所做的一些尝试

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)
python pandas lookup
1个回答
0
投票

你要找的是

merge
但你的预期输出与你的两个输入数据帧不匹配所以也许我错了:

out = df1.merge(df2, on='deviceNames', how='left')
© www.soinside.com 2019 - 2024. All rights reserved.