简体   繁体   中英

How to filter out an entire group based on condition?

I want to remove groups which do not have any values for past year in the created_date column. Here is the data:

+--------+----------------+-----------------------+---------------------+
| class  |     title      |      description      |    created_date     |
+--------+----------------+-----------------------+---------------------+
| ClassA | ClassA Title 1 | Class A Description 1 | 2017-06-20 21:59:07 |
| ClassA | ClassA Title 2 | Class A Description 2 | 2015-06-20 21:59:07 |
| ClassA | ClassA Title 3 | Class A Description 3 | 2014-06-20 21:59:07 |
| ClassB | ClassB Title 1 | Class A Description 1 | 2016-06-20 21:59:07 |
| ClassB | ClassB Title 2 | Class A Description 2 | 2015-06-20 21:59:07 |
| ClassB | ClassB Title 3 | Class A Description 3 | 2014-06-20 21:59:07 |
| ClassC | ClassC Title 1 | Class C Description 1 | 2017-06-20 21:59:07 |
| ClassC | ClassC Title 2 | Class C Description 2 | 2016-06-20 21:59:07 |
| ClassC | ClassC Title 3 | Class C Description 3 | 2015-06-20 21:59:07 |
+--------+----------------+-----------------------+---------------------+

If you see in the above data only group ClassB does not have any created_date for the past year. I want to filter out the entire group ClassB so I end up with only 6 records.

I tried using filter , but not sure what to do with the grouping inside the lamda:

df.groupby(["class"]).filter(lambda group: ...))

Assume your cut off date is date

f = lambda df: not df[df.created_date >= date].empty
df.groupby('class').filter(f)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM