我有两个数据框如下:
import pandas as pd
raw_data = {
'employee_id': ['4444', '5555', '6666','7777','8888'],
'first_name': ['aa', 'Jason', 'Tina', 'Jake', 'Amy'],
'last_name': ['Miller', 'Millers', 'Ali', 'Milner', 'Cooze'],
'age': [42, 42, 36, 24, 73],
}
df1 = pd.DataFrame(raw_data, columns = ['employee_id','first_name', 'last_name', 'age'])
raw_data1 = {'employee_id': ['4444', '5555', '6666','7777'],
'ip': ['192.168.1.101', '192.168.1.102','192.168.1.103','192.168.1.104'],
}
df2 = pd.DataFrame(raw_data1, columns = ['employee_id', 'ip'])
我必须在df1中搜索(比较)df2['employee_id']
,如果值相同,请将df2['ip']
添加到df1中:
print df2['ip'].where(df2['employee_id']==df1['employee_id'])
但这不是正确的方法:
ValueError: Can only compare identically-labeled Series objects
对此问题的任何建议都将不胜感激。
答案 0 :(得分:2)
使用merge
In [1286]: df1.merge(df2, on='employee_id')
Out[1286]:
employee_id first_name last_name age ip
0 4444 aa Miller 42 192.168.1.101
1 5555 Jason Millers 42 192.168.1.102
2 6666 Tina Ali 36 192.168.1.103
3 7777 Jake Milner 24 192.168.1.104
答案 1 :(得分:1)
这是一个更新的答案,因为有人用我认为更好的解决方案删除了帖子。
on = "employee_id"
df3 = df1.set_index(on).join(df2.set_index(on)).fillna("IP missing")
df3["ip"].to_dict()
employee_id first_name last_name age ip
4444 aa Miller 42 192.168.1.101
5555 Jason Millers 42 192.168.1.102
6666 Tina Ali 36 192.168.1.103
7777 Jake Milner 24 192.168.1.104
8888 Amy Cooze 73 IP missing
{'4444': '192.168.1.101',
'5555': '192.168.1.102',
'6666': '192.168.1.103',
'7777': '192.168.1.104',
'8888': 'IP missing'}
上一个回答:
pd.merge(df1,df2,on="employee_id")
https://pandas.pydata.org/pandas-docs/stable/merging.html#database-style-dataframe-joining-merging
pd.merge(左,右,怎么样='内部',on =无,left_on =无, right_on =无, left_index = False,right_index = False,sort = True, 后缀=(' _x',' _y'),copy = True,指示符= False)
给出
employee_id first_name last_name age ip
0 4444 aa Miller 42 192.168.1.101
1 5555 Jason Millers 42 192.168.1.102
2 6666 Tina Ali 36 192.168.1.103
3 7777 Jake Milner 24 192.168.1.104
你可能想要这样的东西:
pd.merge(df1,df2,on="employee_id").set_index("employee_id")["ip"].to_dict()
{'4444': '192.168.1.101',
'5555': '192.168.1.102',
'6666': '192.168.1.103',
'7777': '192.168.1.104'}