Solution Timeline

All versions (edits) of solutions to updating pandas dataframes conditionally appear below in the order they were created. Comments that appear under revisions were those created when that particular revision was current.

To see the revision history of a single solution (with diffs), click on the solution number (ie. "#1") in the upper right corner of a solution revision below.

← Bounty Expand all edits

Hi,

you can try the following command :

df_Full.merge(df_Val, how='left', left_on='id_loc', right_on='id_loc')

If data id_loc is not found in the right table, it will result in a NaN entry

Hi,

you can try the following command :

df_Full.merge(df_Val, how='left', left_on='id_loc', right_on='id_loc')

If data id_loc is not found in the right table, it will result in a NaN entry

edit : it will copy all the column of df_Val to the resulting dataframe

Hi,

you can try the following command :

df_Full.merge(df_Val, how='left', left_on='id_loc', right_on='id_loc')

If data id_loc is not found in the right table, it will result in a NaN entry.
You can have more information on this function using this link
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

edit : it will copy all the column of df_Val to the resulting dataframe

Winning solution

You can simply use a join
df_Full.join(rdf_Val)

The below code works for me.

import pandas as pd
d1 = {'id_loc': [1, 1, 200, 45, 1], 'var1': [1, 2, 3, 4, 5], 'var2': ['a', 'b', 'c', 'd', 'e']}
d2 = {'id_loc': [1, 2, 45, 35, 100, 200, 250], 'var3': ['x', 'v', 'z', 'f', 'j', 'y', 'p']}
df_Full = pd.DataFrame(d1)
df_Var = pd.DataFrame(d2)
df_Full['var3'] = 0
for index, row in df_Full.iterrows():
    temp = (df_Var.loc[df_Var['id_loc'] == row.id_loc]) #row in df_Var matching id_loc from df_Full
    df_Full['var3'].iloc[index] = temp['var3'].values[0]  #get the var3 value
print(df_Full)
Tipped