Home > Enterprise >  pands query only showing true values while grouped
pands query only showing true values while grouped

Time:04-25

I got the following DataFrame:

column1  |  column2
1        | dasd
2        | dasd
1        | 
2        | foo
1        | dasd
2        | bar

I want to group the entry by column1 and only get a list of entries which have more than one unique entry in column2.

So I got this:

df.groupby("column1")["column2"].nunique() > 1

which gives me the following:

column1
1 False
2 True

How do I get a series or list with only the entries in column1 that are True.

CodePudding user response:

I think you can filtering index values by boolean Series:

s = df.groupby("column1")["column2"].nunique() > 1
out = s.index[s].tolist()
print (out)

CodePudding user response:

solved it by

df1 = df.groupby(by="column1", as_index=False).agg({"column2: pd.Series.nunique})
df1[df1.column2 > 1]
  • Related