如何根据条件筛选出整个组?

时间:2017-07-20 17:07:14

标签: python pandas

我想删除created_date列中过去一年没有任何值的群组。这是数据:

+--------+----------------+-----------------------+---------------------+
| 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 |
+--------+----------------+-----------------------+---------------------+

如果您在上述数据中看到,则组ClassB过去一年没有任何created_date。我想过滤掉整个组ClassB,所以我最终只得到6条记录。

我尝试使用filter,但不确定如何处理lamda中的分组:

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

1 个答案:

答案 0 :(得分:3)

假设您的截止日期为date

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