So, I have this table with company names and the value of each order they ordered
Order Id | Company Id | Company Name | Date | Order Value |
---|---|---|---|---|
3455 | 80EYLOKP9E762WKG | Chimera-Chasing | 18-02-2017 | 2345 |
4875 | TLEXR1HZWTUTBHPB | Mellow Ezra | 30-07-2015 | 3245 |
8425 | 839FKFW2LLX4LMBB | Chimera-Chasing | 27-05-2016 | 4566 |
4837 | 97OX39BGVMHODLJM | Worst Mali | 27-09-2018 | 5674 |
3434 | 5T4LGH4XGBWOD49Z | Indonesian Grigory | 14-01-2016 | 7654 |
And, I need to add a new column which will include the segment of each company based on their total orders value
I decided to divide them into 4 segments (Prime, Platinum, Gold, Silver)
So, my approach was to first aggregate this table into a new table with total orders value for each company
with this code:
seg = orders.loc[:,['Company Name', 'Order Value']].groupby('Company Name').sum()
Outcome:
Company Name | Order Value |
---|---|
'48 Wills | 65325 |
10-Day Causes | 85473 |
10-Hour Leak | 83021 |
Youngish Mark'S | 120343 |
10-Year-Old Alba | 97968 |
... | ... |
Then, I used conditions to create new column with segments based on total orders value and added this column to the aggregated data frame "seg"
with this code
conditions = [
(seg['Order Value'] >= 124485),
(seg['Order Value'] >= 105503) & (seg['Order Value'] < 124485),
(seg['Order Value'] >= 88174) & (seg['Order Value'] < 105503),
(seg['Order Value'] < 88174)
]
values = ['Prime', 'Platinum', 'Gold', 'Silver']
seg['Segment'] = np.select(conditions, values)
Now, I need to add this segment column to the original dataframe (orders) with a condition where company name in seg match company name in orders but I dont know how to do that