0

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

2 Answers 2

2

You can check with groupby transform

df['instack']=df['instack'].eq(0).groupby(df['desc']).transform('all')
Sign up to request clarification or add additional context in comments.

2 Comments

What can I say, thankyou very much! Will have to lookup how that transform works, looks very handy. -e- Is there a way to put the transform output into the "inactive" column?
@user3170725 you can just assign it back like what i did
1
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'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.