Reputation: 109
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 ...
Upvotes: 0
Views: 208
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
Reputation: 323226
You can check with groupby
transform
df['instack']=df['instack'].eq(0).groupby(df['desc']).transform('all')
Upvotes: 2