Validation of a conditional rule in a column and duplication in another

Viewed 37

I have a database of products and I have to validate if the product ID repeats in a column and also validate if it's 'True' or 'False' in another column. Then set all to 'True' if at least one of the duplicated rows are 'True'.

I found a way in this link: Create rule for sets of duplicates in a Pandas Dataframe using the second answer, but it's spending too much time doing the process in my database, something like 8min.

Does someone know how to do that in a faster way?

Example:

ID Active 
01 False
01 False
01 True
02 False
02 False
03 True
03 False
03 False

And it should be like this in the end:

ID Active 
01 True
01 True
01 True
02 False
02 False
03 True
03 True
03 True
1 Answers

You can conveniently use max in a groupby.transform:

df['Active'] = df.groupby('ID')['Active'].transform('max')

Or any that is a bit faster:

df['Active'] = df.groupby('ID')['Active'].transform('any')

Output:

   ID  Active
0   1    True
1   1    True
2   1    True
3   2   False
4   2   False
5   3    True
6   3    True
7   3    True
Related