2021年3月3日星期三

Extract the dataframe by multiple conditions in Python

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',]})

enter image description here

  1. cat or dog has to be located at least two places, if not, delete the rows where cat or dog appears once.

The output:

enter image description here

  1. 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:

enter image description here

I am wondering if there is an efficient way to deal with. Thank you so much.

https://stackoverflow.com/questions/66467893/extract-the-dataframe-by-multiple-conditions-in-python March 04, 2021 at 10:50AM

没有评论:

发表评论