Skip to content
Advertisement

Sort the products based on the frequency of changes in customer demand

Imagine following dataframe is given.

JavaScript

I have columns products, custome_demand_date (every week there is new customer demand for products per upcoming months) and months with quantity demand. How can I determine which product has experienced the most frequent changes in customer demand over the months, and sort the products in descending order of frequency of change? I have tried to group by product, accumulate the demand quantity but none of them can analyze the data both horizontally (per customer demand date) and vertically (per months). Desired output:

JavaScript

Either ranking or % of change frequency or count of changes.

  • Note: percentages in desired output are random numbers

I’d really appreciate if you have any clever approach to solve this problem? Thanks

Advertisement

Answer

One way is to define a function that counts horizontal and vertical changes which you can apply to each group individually.

JavaScript

This returns

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