An Excellent Method For Accurate Data Proportions, DF GroupBy PercentChange

A step by step guide to use pandas in exploratory data analysis

If you want to reorganize a dataframe into logical groups and find the percentage change of a value row over row, then group by percentage change is a perfect choice.

Let’s see how it transforms the data

Developer Notes

Dataframe groupby pct_change() method calculates the percentage change of a value to the previous entry in the group.

Does it depend on the position of the row inside a group?

Yes. The percentage change is calculated by comparing the value in the current row with the corresponding value in the previous row. So if the sorting order changes, the previous row may also change. Then the percentage change will differ.

Can I calculate percentage change for multiple columns in one go?

Yes. Once the group by is done, pass multiple columns as a list and then call the pct_change() function. It calculates the percentage change for each column in comparison with its corresponding previous entry in the group and returns a dataframe.

Why the first row always shows NA?

That’s because it has no previous value to compare. Just replace the na with an empty string in your final dataframe.

Let’s see how it supports key decisions related to price proportionality in the retail sector.

Business Case

A value-based pricing policy prescribes that the products have to be priced in proportion to the customer feedback. A product’s price should be proportionately higher than the product that has received immediate lower feedback. In other words, if a product receives lower feedback, subsequently its price has to be lowered proportionately.

A customer expects a better discount and a lower price for the products that got lower feedback from the previous customers. It is very important for the store managers to keep revising the product prices accurately on par with this feedback. Otherwise, there will be a wastage loss and a revenue loss. If the product’s price is higher than the expected price, it cannot be sold fast and results in wastage. On the other hand, if the product’s price is lower than the expected price, there will be a revenue loss.

In summary, the business needs a system that indicates the customer’s expected price for every product.

It is a very good opportunity for the python developers to add business value by preventing both these losses.

Let me explain it in detail:


  1. Firstly, get the price details of various products as per their brand and model. Take the aggregated customer feedback for every brand-model into the last column.
  1. Then sort the records by ‘customer feedback’. Because, this is the column that indicates the expected price.
  2. Group by the product title
  3. Calculate the perctange change in the feedback and add it to a new column
  4. Calcualte the percentage change in the price.
  1. Ideally both the percentage changes should tally as per the policy. But this is not an ideal world.
  2. Calculate the different between price change and feedback change.
  3. If the difference is positive, there is some scope to increase the price and prevent the revenue loss.
  4. If the difference is negative, the product in over priced as per customers expectation. Reduce the price to clear the inventory faster.
  5. Calculate the expected price from the magnitude of the difference.

After executing the above code, the final dataframe would look as follows:


We are able to instantly detect the price gaps and prevent revenue loss due to differences between the actual and expected prices.

What Next?

If you would like to share your use case, relevant to this discussion, please send the details to Our editorial team will get in touch to add your contribution to this story.

An example speaks a thousand paragraphs. Let’s build together a new generation of fast learners.

Also Read

An Excellent Method for Instant Data Transformation, DF GroupBy Transform

How is GroupBy Rank used in Pricing Intelligence?

Write a Comment