Pandas Dataframe looks like so:
Col1 Col2
A 1
A 1
A 1
B 0
B 0
B 1
B 1
B 1
C 1
C 1
C 1
C 1
I wanted to group all together in Col1
, then check Col2
to see whether all values for that group i.e. A are 1. In this example the desired output would be:
[A, C]
(because only A and C have all values set to 1). How do I do this?
In your case groupby
with all
df.groupby('Col1').Col2.all().loc[lambda x : x ].index.tolist()
Out[350]: ['A', 'C']
Or without groupby
df.loc[~df.Col1.isin(df.Col1[df.Col2.eq(0)]),'Col1'].unique()
Out[352]: array(['A', 'C'], dtype=object)
From the comment
cs95 :df.loc[df['Col2'].astype(bool).groupby(df['Col1']).transform('all'), 'Col1'].unique()
df.loc[df['Col2'].astype(bool).groupby(df['Col1']).transform('all'), 'Col1'].unique()
[*{*df.Col1[df.Col2.eq(1).groupby(df.Col1).transform(all)]}]
@WeNYoBen: I used the first solution and it worked. I didn't try the other solutions, but I'll mark them up anyway. Thanks!