python - Return value from other dataframe from partial string match -


i'm trying create new dataframe column partial string match dataframe. how example below?

df1: #   id 1   666666 2   666667 3   666668 4   666667  df2 #   ref 1   ref_666666_blah blah 2   ref_666667_blah blah 3   ref_666668_blah blah 4   ref_666667_blah blah  df3 #what want #   id      match 1   666666  ref_666666_blah blah 2   666667  ref_666667_blah blah 3   666668  ref_666668_blah blah 4   666667  ref_666667_blah blah 

i know not code i'm trying below:

df1['match'] = df2['ref'].map(lambda x: x if x.str.contains(df1['match']) 

thanks!

there number of ways accomplish this.

if able extract id ref column, in particular example df2[id] = df2.ref.apply(lambda c: c.split('_')[1]), proceed df1.join(df2, on = 'id').

if need call more complicated match function, following:

def getmatch(str_id):     matches = (c c in df2['ref'] if str_id in c)     try:         return matches.next()     except:         return none  df1['match'] = df1['id'].apply(getmatch) 

this result in number of redundant comparisons, should think if there relationships in data can simplify match. instance, if each ref matches @ 1 id or if can somehow sort both dataframes in meaningful way , merge them recursively.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -