I have been searching everywhere on stack for this question and answer but I can’t seem to find it anywhere.
I have a pandas dataframe which looks like the example below:
product | purchase | price |
---|---|---|
credit | ||
toy | cash | £20 |
electronics | cash | £50 |
groceries | cash | £80 |
gaming | cash | £30 |
cash | ||
toy | credit | £20 |
electronics | credit | £50 |
groceries | credit | £80 |
gaming | credit | £30 |
transfer | ||
toy | cash | £20 |
electronics | cash | £50 |
groceries | cash | £80 |
gaming | cash | £30 |
So I tried to make a dataframe above to show you what I mean. Essentially in the dataframe above I want the values in the 2nd column to be replaced by the 1st corresponding value at the top of each group.
So for the first group the value is credit but the second column for the first group of items is “cash” can I create a function which takes the first value at the top of the group and assigns all the cash values for only that group to credit based on the first item in that group?
And then the same for the second group where the first item is cash, I want it to take in the first item of the second group and replace all the credit values for that group to the first item of the second group which in this case would be cash.
And so on down the list?
Apologies if this is not very clear but if anyone can help solve this I will be extremely grateful. :)
What I would like to see in the output: :)
product | purchase | price |
---|---|---|
credit | ||
toy | credit | £20 |
electronics | credit | £50 |
groceries | credit | £80 |
gaming | credit | £30 |
cash | ||
toy | cash | £20 |
electronics | cash | £50 |
groceries | cash | £80 |
gaming | cash | £30 |
transfer | ||
toy | transfer | £20 |
electronics | transfer | £50 |
groceries | transfer | £80 |
gaming | transfer | £30 |
Thank Youuuuu