user3170725
user3170725

Reputation: 109

Python - Comparing multiple columns

I am trying to deactivate certain products from inventory if all their sizes have zero stock, from a csv using pandas. Psuedo - group all the products by "desc" and if all "instock" equals zero then return true to "inactive"

I have tried using groupby function with pandas, but can't get my head around how it works.

Here is the csv, and me trying to explain what I mean ...

enter image description here

Upvotes: 0

Views: 208

Answers (2)

manwithfewneeds
manwithfewneeds

Reputation: 1167

df.loc[df.groupby('desc')['instock'].transform(sum).eq(0), 'inactive'] = 'T'

The groupby/transform returns a Boolean mask where the sum of the instock series per group equals zero. This get's wrapped in a dataframe loc statement to choose only those rows where the mask is True, and sets the inactive column to 'T'.

Upvotes: 1

BENY
BENY

Reputation: 323226

You can check with groupby transform

df['instack']=df['instack'].eq(0).groupby(df['desc']).transform('all')

Upvotes: 2

Related Questions