我有两个数据帧: 用户
user_id position
0 201 Senior Engineer
1 207 Senior System Architect
2 223 Senior account manage
3 212 Junior Manager
4 112 junior Engineer
5 311 junior python developer
df1 = pd.DataFrame({'user_id': ['201', '207', '223', '212', '112', '311'],
'position': ['Senior Engineer', 'Senior System Architect', 'Senior account manage', 'Junior Manager', 'junior Engineer', 'junior python developer']})
角色
role_id role_position
0 10 %senior%
1 20 %junior%
df2 = pd.DataFrame({'role_id': ['10', '20'],
'role_position': ['%senior%', '%junior%']})
我想加入他们,使用如下条件为df1中的每一行获取角色\u id:
lower(df1.position) LIKE df2.role_position
我想使用操作符LIKE(比如在SQL中)。 所以它看起来是这样的(或者没有角色_的位置-会更好):
user_id position role_id role_position
0 201 Senior Engineer 10 %senior%
1 207 Senior System Architect 10 %senior%
2 223 Senior account manage 10 %senior%
3 212 Junior Manager 20 %junior%
4 112 junior Engineer 20 %junior%
5 311 junior python developer 20 %junior%
我怎么做这个? 谢谢你的帮助
如果资历级别始终从前面开始,则直接执行
merge
可以避免一些麻烦:否则,您可以在
merge
期间执行pd.Series.str.extract
:两者产生相同的结果:
您可以使用
str.extract()
+merge()
:df1
的输出:可能性:
相关问题 更多 >
编程相关推荐