1
讓數據幀是這樣的:掉落組GROUPBY不包含的元件(Python的熊貓)
import pandas as pd
df = pd.DataFrame({"name":["A", "A", "B" ,"B", "C", "C"],
"nickname":["X","Y","X","Z","Y", "Y"]}
我怎樣才能組DF和下降並不以包含那些基團(C)至少有一個'X'?
謝謝
讓數據幀是這樣的:掉落組GROUPBY不包含的元件(Python的熊貓)
import pandas as pd
df = pd.DataFrame({"name":["A", "A", "B" ,"B", "C", "C"],
"nickname":["X","Y","X","Z","Y", "Y"]}
我怎樣才能組DF和下降並不以包含那些基團(C)至少有一個'X'?
謝謝
您可以使用filter
從大熊貓的分組:
df.groupby('name').filter(lambda g: any(g.nickname == 'X'))
# name nickname
# 0 A X
# 1 A Y
# 2 B X
# 3 B Z
謝謝Psidom。我不知道「任何」功能 – dleal