i have a dataframe that looks like the one given below.
item_id length height shape color
a True
a True
a False
a True
a True
b True
b True
b False
b True
i want to do something like
data_df.groupby('item_id').all().reset_index()
to convert the dataframe to
item_id length height shape color
a True False True False
b True True False True
my problem is with color for item a which should be false as it does not exist but it comes out as true.
CodePudding user response:
Does this work for you?
df.groupby('item_id').aggregate(lambda x : False if x.isnull().all() else x.all())
Output
length height shape color
item_id
a True False True False
b True True False True
CodePudding user response:
You can work on the inverse, which consider blank cells as True
:
(~df.drop('item_id', axis=1).eq(False)).groupby(df['item_id']).all()
Output:
length height shape color
item_id
a True False True True
b True True False True
Or if you want to consider blank cells as False
, you can convert the data to boolean before groupby:
df.drop('item_id',axis=1).astype(bool).groupby(df['item_id']).all()
Output:
length height shape color
item_id
a False False False False
b False False False False