Home > Mobile >  python groupby column and then getting any false value in each group
python groupby column and then getting any false value in each group

Time:09-26

dff = (dff.groupby(["column 1", "column 2"]).first()) <br/>
print(dff)

gives table

column 1 column 2 column 3
First a true
b false
c true
d false
second a true
b true
c true
Third a true
b false
c true
d false
e false

above table was obtained by grouping 2 columns out of 3 columns

column 1 column 2 column 3 column 4
First a true false
b false
c true
d false
second a true true
b true
c true
Third a true false
b false
c true
d false
e false

as shown above we want to have one more column with single value
if any one of the values in column 3 ( at group level) has false , value should be false..else it should be true

pls help as i am stuck from 2 days

CodePudding user response:

Try this:

>>> df.groupby('col1')['col3'].all()
col1
First     False
Second     True
Third     False
Name: col3, dtype: bool

CodePudding user response:

You can use .groupby() .transform() min, as follows:

dff['column 4'] = dff.groupby(['column 1'])['column 3'].transform('min')

min of a set of True/False values will give False if there is at least one False value. Otherwise, it will give True.

Data Input

data = {'column 1': ['First', 'First', 'First', 'First', 'second', 'second', 'second', 'Third', 'Third', 'Third', 'Third', 'Third'],
 'column 2': ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'a', 'b', 'c', 'd', 'e'],
 'column3': [True, False, True, False, True, True, True, True, False, True, False, False]}

dff = pd.DataFrame(data).set_index(['column 1', 'column 2'])


                   column3
column 1 column 2         
First    a            True
         b           False
         c            True
         d           False
second   a            True
         b            True
         c            True
Third    a            True
         b           False
         c            True
         d           False
         e           False

Result:

print(dff)

                  column 3  column 4
column 1 column 2                   
First    a            True     False
         b           False     False
         c            True     False
         d           False     False
second   a            True      True
         b            True      True
         c            True      True
Third    a            True     False
         b           False     False
         c            True     False
         d           False     False
         e           False     False

  • Related