I would like to use pandas.groupby certain way. Given a DataFrame with two boolean columns (call them col1 and col2 ) and an id column, I want to add a column as follows:
for each record, if ( col2 - True), and ( col1 is True for any of the records with the same identifier), then assign True. Otherwise, False.
I made a simple example:
df = pd.DataFrame([[0,1,1,2,2,3,3],[False, False, False, False, False, False, True],[False, True, False, False, True ,True, False]]).transpose() df.columns = ['id', 'col1', 'col2']
gives the following DataFrame :
id col1 col2 0 0 False False 1 1 False True 2 1 False False 3 2 False False 4 2 False True 5 3 False True 6 3 True False
In accordance with the above rule, add the following column:
0 False 1 False 2 False 3 False 4 False 5 True 6 False
Any ideas on an elegant way to do this?