For each element in a group determine if it is present in the next group (in order as these groups appear - not necessarily numerical). For the last group - all False
.
Example:
df = pd.DataFrame({'group': [ 0, 1, 1, 0, 2 ],
'val': ['a', 'b', 'a', 'c', 'c']})
grouped = df.groupby('group')
print(result)
0 True
1 False
2 False
3 False
4 False
Name: val, dtype: bool
What is the best way to do it? I can accomplish it like this, but it seems too hacky:
keys = list(grouped.groups.keys())
iterator_keys = iter(keys[1:])
def f(ser):
if ser.name == keys[-1]:
return ser.isin([])
next_key = next(iterator_keys)
return ser.isin(grouped.get_group(next_key)['val'])
result = grouped['val'].apply(f)