Subway_Hacker's solution to "updating pandas dataframes conditionally"

The below code works for me.<\/p>\n

import<\/span> pandas<\/span> as<\/span> pd<\/span>\nd1<\/span> =<\/span> {<\/span>'id_loc'<\/span>:<\/span> [<\/span>1<\/span>,<\/span> 1<\/span>,<\/span> 200<\/span>,<\/span> 45<\/span>,<\/span> 1<\/span>],<\/span> 'var1'<\/span>:<\/span> [<\/span>1<\/span>,<\/span> 2<\/span>,<\/span> 3<\/span>,<\/span> 4<\/span>,<\/span> 5<\/span>],<\/span> 'var2'<\/span>:<\/span> [<\/span>'a'<\/span>,<\/span> 'b'<\/span>,<\/span> 'c'<\/span>,<\/span> 'd'<\/span>,<\/span> 'e'<\/span>]}<\/span>\nd2<\/span> =<\/span> {<\/span>'id_loc'<\/span>:<\/span> [<\/span>1<\/span>,<\/span> 2<\/span>,<\/span> 45<\/span>,<\/span> 35<\/span>,<\/span> 100<\/span>,<\/span> 200<\/span>,<\/span> 250<\/span>],<\/span> 'var3'<\/span>:<\/span> [<\/span>'x'<\/span>,<\/span> 'v'<\/span>,<\/span> 'z'<\/span>,<\/span> 'f'<\/span>,<\/span> 'j'<\/span>,<\/span> 'y'<\/span>,<\/span> 'p'<\/span>]}<\/span>\ndf_Full<\/span> =<\/span> pd<\/span>.<\/span>DataFrame<\/span>(<\/span>d1<\/span>)<\/span>\ndf_Var<\/span> =<\/span> pd<\/span>.<\/span>DataFrame<\/span>(<\/span>d2<\/span>)<\/span>\ndf_Full<\/span>[<\/span>'var3'<\/span>]<\/span> =<\/span> 0<\/span>\nfor<\/span> index<\/span>,<\/span> row<\/span> in<\/span> df_Full<\/span>.<\/span>iterrows<\/span>():<\/span>\n    temp<\/span> =<\/span> (<\/span>df_Var<\/span>.<\/span>loc<\/span>[<\/span>df_Var<\/span>[<\/span>'id_loc'<\/span>]<\/span> ==<\/span> row<\/span>.<\/span>id_loc<\/span>])<\/span> #row in df_Var matching id_loc from df_Full<\/span>\n    df_Full<\/span>[<\/span>'var3'<\/span>]<\/span>.<\/span>iloc<\/span>[<\/span>index<\/span>]<\/span> =<\/span> temp<\/span>[<\/span>'var3'<\/span>]<\/span>.<\/span>values<\/span>[<\/span>0<\/span>]<\/span>  #get the var3 value<\/span>\nprint<\/span>(<\/span>df_Full<\/span>)<\/span>\n<\/pre><\/div>

User: Subway_Hacker

Question: updating pandas dataframes conditionally

Back to question