I'm trying to find an efficient way of finding duplicated rows when a value represents a set of values:
df = pd.DataFrame(
{
"ID": ["one", "two", "two", "two", "one"],
"condition1": ["all", "red", "all", "red", "red"],
"condition2": ["yellow", "black", "black", "orange", "black"],
}
)
In this case, 'all' represent all colors in the present column, so the ID 'two' is duplicated because we have: [two,red,black] and [two,all,black] where "all" can be black. But the key "one" is not duplicated, because in the second condition is not the same color.
The best way I figured out is exploding the columns and calling duplicated(), the problem is that this solution makes an extremely large dataframe in normal use cases.
df.loc[df.condition1=='all','condition1'] = set(df.condition1)-{'all'}
df.loc[df.condition2=='all','condition2'] = set(df.condition2)-{'all'}
df = df.explode('condition1')
df = df.explode('condition2')
df.duplicated()
Is there any more standard/ efficient way to solve this? I was also thinking in a groupby that accepts the keyword 'all' as all possibilities, or maybe iterating by IDs and individually checking where values are 'all'.
Edit: maybe this solution, splitting and iterated per ID, could do the trick well enough, but maybe there are still better ways.