Two dataframes are there, df1 has 2 columns,name & profession
df_1
Name profession
srinu senior engineer
Azahar engineer
vijaya data analyst
rahul team lead
swapna manager
krishna engineer
rama senior engineer
df_2 has only one column with (name-employeeid) df_2
Name-empid
vijaya-2124148
rahul-2124152
krishna-2124189
rama-2124169
I am trying to compare df1 and df2 using name and if names in df2 matches with df1, profession should be replaced as"data scientist".
i have tried many things,but couldn't figure it out,can someone help me,please?
i have tried using map,replace..but getting errors
You could use:
# extract the names from df_2
m = (df_1['Name']
.str.lower()
.isin(df_2['Name-empid'].str.extract('(\w+)-', expand=False))
)
# match with df_1 ensuring common case
df_1.loc[m, 'profession'] = 'Data Scientist'
Output:
Name profession
0 srinu senior engineer
1 Azahar engineer
2 vijaya Data Scientist
3 rahul Data Scientist
4 swapna manager
5 krishna Data Scientist
6 rama Data Scientist
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.