Oct-15-2020, 08:08 PM
I think I got it. Used a 2 pass approach - first pass to pick up the codes and values to be set in a dictionary. Second pass to set the values.
import pandas as pd import numpy as np df = pd.DataFrame(data = {'Code':[100,100,100,200,200], 'Value':[np.nan,1000,2000,np.nan,2000], 'ValueCount':[np.nan,1,2,np.nan,1], 'Expected_Cum_Value':[1000,1000,1000,2000,2000],}) df['Result_Value'] = '' code_list = dict() for row in df.itertuples() : if row.ValueCount == 1 : code_list[row.Code] = row.Value for row_index in range(len(df)) : if df.iloc[row_index,0] in code_list: df.iloc[row_index,4] = code_list[df.iloc[row_index,0]] df
Output: Code Value ValueCount Expected_Cum_Value Result_Value
0 100 NaN NaN 1000 1000
1 100 1000.0 1.0 1000 1000
2 100 2000.0 2.0 1000 1000
3 200 NaN NaN 2000 2000
4 200 2000.0 1.0 2000 2000