dleal
dleal

Reputation: 2314

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

Upvotes: 7

Views: 4864

Answers (1)

akuiper
akuiper

Reputation: 215047

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

Upvotes: 14

Related Questions