我必须为空两个dataframes:搜索在熊猫数据帧
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
对此问题的任何建议w不胜感激。
如果我想向df1添加匹配值,那么怎么办:添加一列并插入匹配的ip,不匹配将是空的。谢谢 – jojo
@jojo在此重新分配它:on =“employee_id”,df1 = df1.set_index(on).join(df2.set_index(on))。reset_index() –
您的数据科学知识非常棒。你能否建议一些书籍或视频教程?我是Python开发人员,但在数据科学领域是全新的。谢谢 – jojo