'Python/Pandas: Filling new column after join
I have a single data frame containing a lot of information, the columns I am interested in have the following format after doing a left join in Pandas:
Company Product Brand Model Bonus Bonus+ Price
A X Sony xxx False False 100
A X Sony xxx True False 110
A X Sony xxx True True 115
A X Samsung sth False False 95
... ... ... ... ... ... ...
I did a join on the company, product brand, and model to get an ordered data frame, now what I need is based on the above data frame, I have to calculate the difference in price between no bonus, bonus, and bonus plus in new 2 new columns (bonus difference from none, and bonus plus from bonus).
My current code is:
merged = df.merge(df, on=["Company","Product","Make","Series"], how='left')
I had a previous code where I did a loop for each unique company and their product, brand, and model to create small data frames containing the very narrowed down info and then input the calculation. But it takes too long and it is not optimized.
My question is, how can I add the price difference between no bonus and bonus, and bonus and bonus plus for a set of company, product, brand, and model? Outcome should look like this:
Company Product Brand Model Bonus Bonus+ Price BonusPrice
A X Sony xxx False False 100 -
A X Sony xxx True False 110 10
A X Sony xxx True True 115 -
A X Samsung sth False False 95
... ... ... ... ... ... ...
Bonus+Price
-
-
5
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
