|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import seaborn as sns |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | +sales_dict = {'colour': ['Yellow', 'Black', 'Blue', 'Red', 'Yellow', 'Black', 'Blue', |
| 7 | + 'Red', 'Yellow', 'Black', 'Blue', 'Red', 'Yellow', 'Black', 'Blue', 'Red', 'Blue', 'Red'], |
| 8 | + 'sales': [100000, 150000, 80000, 90000, 200000, 145000, 120000, |
| 9 | + 300000, 250000, 200000, 160000, 90000, 90100, 150000, 142000, 130000, 400000, 350000], |
| 10 | + 'transactions': [100, 150, 820, 920, 230, 120, 70, 250, 250, 110, 130, 860, 980, 300, 150, 170, 230, 280], |
| 11 | + 'product': ['type A', 'type A', 'type A', 'type A', 'type A', 'type A', 'type A', |
| 12 | + 'type A', 'type A', 'type B', 'type B', 'type B', 'type B', 'type B', 'type B', 'type B', |
| 13 | + 'type B', 'type B']} |
| 14 | + |
| 15 | +data_sales = pd.DataFrame(sales_dict) |
| 16 | +# print(data_sales) |
| 17 | + |
| 18 | +ref_data = data_sales[data_sales.colour == 'Blue'][['sales', 'product']] |
| 19 | +ref_data = ref_data.rename(columns={"sales": "sales_1"}) |
| 20 | + |
| 21 | +def make_color(d, ref_data): |
| 22 | + # d['sales_2'] = d['sales'] - 1 |
| 23 | + d = d.merge(ref_data, how='inner', on='product') |
| 24 | + return d |
| 25 | + |
| 26 | +data_colour = data_sales.groupby('colour').apply(lambda x: make_color(x, ref_data)) |
| 27 | +print(data_colour) |
0 commit comments