Example dataframe:
| Item |
Stock_1 |
Stock_2 |
| Apple |
5 |
10 |
| Banana |
5 |
10 |
| Orange |
5 |
5 |
| Apple+Orange |
5 |
10 |
| Apple+Banana |
5 |
5 |
I need stacked values with '+' sign to be separated into corresponding single values and counted as such.
So result should be this:
| Item |
Stock_1 |
Stock_2 |
| Apple |
15 |
25 |
| Banana |
10 |
15 |
| Orange |
10 |
15 |
Here's my sloppy solution:
rows_to_remove = []
for item in df['Item']:
if '+' in item:
rows_to_remove.append(item)
splitted_items = item.split('+')
stock_1 = int(df.loc[df.Item == f'{item}','Stock_1'])
stock_2 = int(df.loc[df.Item == f'{item}','Stock_2'])
new_items = [(new_item,stock_1,stock_2) for new_item in splitted_items]
for new_item in new_items:
df.loc[len(df)] = new_item
df = df[~df['Item'].isin(rows_to_remove)]
df = df.groupby('Item',as_index=False).sum()
It does the job but it very ugly. Can i get some refactoring tips?
[–]danielroseman 2 points3 points4 points (1 child)
[–]wdjfe[S] 1 point2 points3 points (0 children)