How might I get the value for each Key, given that Type is equal to B and assign that result to every A? I need to apply this logic to a large dataframe with many different groups.
df = pd.DataFrame({'Key': [1, 2, 3, 4, 5],
'Group': ['Z', 'Z', 'Z', 'Z', 'Z'],
'Type': ['A', 'A', 'B', 'A', 'A'],
'Flag': [0, 0, 1, 0, 0]
})
Expected outcome:
expected = pd.DataFrame({'Key': [1, 2, 3, 4, 5],
'Group': ['Z', 'Z', 'Z', 'Z', 'Z'],
'Type': ['A', 'A', 'B', 'A', 'A',],
'parentKey': [3, 3, np.nan, 3, 3],
'Flag': [0, 0, 1, 0, 0]})
I have been playing with transform but am rather stumped and not getting anywhere close.