Skip to content
Advertisement

Python Pandas count function on condition and subset

i have a dataframe like this

JavaScript

I want to build the following count function to count the items in my dataframe like shown below.

  • The Function should count by the Subset ['F_Class','Product']
  • If df['Packages'] == 2 then increase by +2 else increase by +1

The result should look like this:

JavaScript

Advertisement

Answer

If need sum by Packages numbers use DataFrameGroupBy.cumsum with replace missing values to 1:

JavaScript

Detail:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement