Skip to content
Advertisement

change value by key of another dataframe

I think it is very easy and simple question. but it is very difficult for me. please help! I can write R code

b$v2 <- ifelse(a$key==b$key, a$v2, b$v2)

input df a is a df, it have key and answer value. b is a df, it have to change according to a.

a1=[11,22,44]
a2=['a','b','d']
a3={'key':a1, 'v2':a2}
a=pd.DataFrame(a3)

b1=[11,22,33,44,55]
b2=['ff','gg','c','hh','e']
b3={'key':b1, 'v2':b2}
b=pd.DataFrame(b3)

final result (what I want)

c1=[11,22,33,44,55]
c2=['a','b','c','d','e']
c3={'key':c1, 'v2':c2}
c=pd.DataFrame(c3)

but I don’t know how to write python. I tried.

My concept

first, making True, False

i=1
a['key'].iloc[i]==b['key']

I have 5 bools (F, T, F, F, F)

second, When True value, Change ‘v2’ col of df b but it is not working

b['v2'] = np.where(a['key'].iloc[i]==b['key'], a['v2'], b['v2'])

error message :

ValueError: operands could not be broadcast together with shapes (5,) (3,) (5,)

I think I have a misunderstanding using ‘np.where’ function.

Advertisement

Answer

we could use Series.map for mapping key -> v2. Set a‘s key as index using set_index. Now, use it in Series.map

m = a.set_index('key')['v2']
b['v2'] = b['key'].map(m).fillna(b['v2'])

print(b)
#    key v2
# 0   11  a
# 1   22  b
# 2   33  c
# 3   44  d
# 4   55  e
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement