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

JavaScript

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

JavaScript

final result (what I want)

JavaScript

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

My concept

first, making True, False

JavaScript

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

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

JavaScript

error message :

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement