Skip to content
Advertisement

How to extract ids and rows only if a column has all of the designated values

I have the following dataframe

JavaScript

I want to group by id and keep those ids if it contains all of the designated values (i.e. 2019Q4, 2020Q4, 2021Q4) then extract rows that correspond to those values. isin() won’t work because it won’t drop C and D.

desired output

JavaScript

Advertisement

Answer

You can use set operations to filter the id and isin for the date:

JavaScript

or, using transform:

JavaScript

output:

JavaScript

id_ok:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement