Skip to content
Advertisement

How to replace cost of an item with the previous cost of the same item in a dataframe using Pandas?

Suppose I have the following dataframe:

JavaScript

enter image description here

And I want to replace the cost of the current item with the cost of the previous item using Pandas, with the first instance of each item being deleted. So the above dataframe would become

JavaScript

enter image description here

What’s a good way to do it?

Advertisement

Answer

You can use groupby on Item as well. This gives you output in the same order you expected

JavaScript

This gives us expected output:

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