input:

df1:
name, msg_time, age
adam,22-00-11, Nan
susam,23-00-14,Nan

df2:
name, age
adam, 23

i want to add msg_time from df1 to df['age'] for specific name

output:

df2:

name, age
adam, 23,22-00-11

my code:

df2=df2.assign(age=lambda x: x.age +','+df1.msg_time)

my problem is, that i am receiving :

df2:
name, age
adam, 23,22-00-11,22-00-11,22-00-11,22-00-11

3 Answers

1
anky_91 On Best Solutions

you could do it using series.map() and converting to string before concatenating them using astype(str):

df2.age=df2.age.astype(str)+','+df2.name.map(df1.set_index('name')['msg_time']).astype(str)
print(df2)

   name          age
0  adam  23,22-00-11
2
yatu On

You could merge on name and join the fields of interest separated by ,:

(df1.merge(df2, on='name')
    .set_index('name')[['age_y', 'msg_time']]
    .astype(str)
    .apply(','.join, axis=1)
    .rename('age')
    .reset_index())

   name          age
0  adam  23,22-00-11
2
Wen-Ben On

I am using combine_first

df2.set_index('name').combine_first(df1.set_index('name')).dropna().astype(str).apply(' '.join,1).to_frame('age')
                age
name               
adam  23.0 22-00-11