grouped['age'].filter(lambda x: x>32)

直接报错:TypeError: the filter must return a boolean result

因为分组之后,每组可能有多条数据,这样导致x>32结果有多个布尔值。需要使用all()或者any()。

grouped['age'].filter(lambda x: (x>32).any())
grouped['age'].filter(lambda x: (x>32).all())

另外多个条件的话,需要使用&或|来连接。pandas里使用&或|代替and或or。

 posted on 2024-02-01 21:04  会飞的金鱼  阅读(39)  评论(0)    收藏  举报