I have a simple dataset, but I need to extract a sub-dateset under multiple conditions (by order):
df = pd.DataFrame({'animal': ['cat','cat','cat','dog','bird','bird'], 'place': ['A','B','C','A','B','C',]})
- cat or dog has to be located at least two places, if not, delete the rows where cat or dog appears once.
The output:
- cat or dog has to be in A place, if not, delete the rows. For example, if cat only stays in B or C, delete all rows of cat, but if cat stays A, and (B or C) which means A,B, A,C, or A,B,C, keep all cat rows.
The final output:
I am wondering if there is an efficient way to deal with. Thank you so much.


