删除groupby中不包含元素的组(Python Pandas)

时间:2016-06-27 02:45:13

标签: python pandas

让数据框如下所示:

import pandas as pd

df = pd.DataFrame({"name":["A", "A", "B" ,"B", "C", "C"],
                   "nickname":["X","Y","X","Z","Y", "Y"]}

如何对df进行分组并删除那些不包含至少一个“X”的组(C)?

谢谢

1 个答案:

答案 0 :(得分:5)

您可以使用pandas中的df.groupby('name').filter(lambda g: any(g.nickname == 'X')) # name nickname # 0 A X # 1 A Y # 2 B X # 3 B Z 分组:

RewriteRule ^ct-menu-item-3$ about-us [NC,R=301,L]