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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With