1

I have two data frames A,B

"A" data frame consists of 1 column(column name:str1)

"B" data frame consists of 2 columns(column names:m2,m3)

I am comparing str1 from "A" with m2 of "B"

what i want is when "str1" matches with "m2" i want "str1" to get replaced by "m3". following is my data,

    data frame A.str1="gmaps","facebook","gmail","linkedin"
    data frame B.m1  ="gmaps","oracle","gmail","intel"
    data frame B.m2  ="Google","xyz","Google","nvidia"

i want the output as

    data frame A.str1="Google","facebook","Google","linkedin"

so where ever A.str1 matches with B.m1 the A.str1 will be replaced by B.m2 thanks in advance

sathvik
  • 55
  • 1
  • 5
  • Please add some sample data and expected output to this question. See this [SO Post](https://stackoverflow.com/a/20159305/6361531) on how to ask a good pandas question. – Scott Boston Mar 11 '18 at 04:12

1 Answers1

1

I think you need create helper Series and replace or use map, but it creates NaNs for non matched columns, so is necessary fillna or combine_first:

df1 = pd.DataFrame({'str1': ["gmaps","facebook","gmail","linkedin"]})

df2 = pd.DataFrame({'m1': ["gmaps","oracle","gmail","intel"],
                    'm2': ["Google","xyz","Google","nvidia"]})

s = df2.set_index('m1')['m2']
print (s)
m1
gmaps     Google
oracle       xyz
gmail     Google
intel     nvidia
Name: m2, dtype: object

df1['str1'] = df1['str1'].replace(s)
#alternatives
#df1['str1'] = df1['str1'].map(s).fillna(df1['str1'])
#df1['str1'] = df1['str1'].map(s).combine_first(df1['str1'])
print (df1)

       str1
0    Google
1  facebook
2    Google
3  linkedin
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252