dleal dleal - 1 year ago 116
Python Question

Drop groups in groupby that do not contain an element (Python Pandas)

Let a data frame be like the following:

import pandas as pd

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

How can I group df and drop those groups (C) that do not contain at least one 'X'?

thank you

Answer Source

You can use the grouped by filter from 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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download