I have the following dataframe:
df = pd.DataFrame(
[{'price': 22, 'weight': 1, 'product': 'banana', },
{'price': 20, 'weight': 2, 'product': 'apple', },
{'price': 18, 'weight': 2, 'product': 'car', },
{'price': 100, 'weight': 1, 'product': 'toy', },
{'price': 27, 'weight': 1, 'product': 'computer', },
{'price': 200, 'weight': 1, 'product': 'book', },
{'price': 200.5, 'weight': 3, 'product': 'mouse', },
{'price': 202, 'weight': 3, 'product': 'door', },]
)
What I have to do is to group by contiguous prices where the difference between them is less than a threshold (say 2.0) or not. After that I have to apply the following aggregations ONLY on the group 'less than threshold', otherwise the group should not be aggregated:
priceshould be the weighted average betweenpriceandweightweightshould be the maximum valueproductshould be string concatenation
What I did so far (step by step):
- I sorted the dataframe by prices in ascending order (to get the contiguous values)
df.sort_values(by=['price'], inplace=True)
price weight product
2 18.0 2 car
1 20.0 2 apple
0 22.0 1 banana
4 27.0 1 computer
3 100.0 1 toy
5 200.0 1 book
6 200.5 3 mouse
7 202.0 3 door
- Got the difference between the prices in ascesding and descending order to detect the contiguous prices
df['asc_diff'] = df['price'].diff(periods=1)
df['desc_diff'] = df['price'].diff(periods=-1).abs()
price weight product asc_diff desc_diff
2 18.0 2 car NaN 2.0
1 20.0 2 apple 2.0 2.0
0 22.0 1 banana 2.0 5.0
4 27.0 1 computer 5.0 73.0
3 100.0 1 toy 73.0 100.0
5 200.0 1 book 100.0 0.5
6 200.5 3 mouse 0.5 1.5
7 202.0 3 door 1.5 NaN
- Combined
asc_diffanddesc_diffcolumns to removeNaNand create the continous regions
df['asc_diff'] = df['asc_diff'].combine_first(df['desc_diff'])
df['asc_diff'] = df[['asc_diff', 'desc_diff']].min(axis=1).abs()
df['asc_diff'] = df['asc_diff'] <= 2.0
df = df.drop(columns=['desc_diff'])
price weight product asc_diff
2 18.0 2 car True
1 20.0 2 apple True
0 22.0 1 banana True
4 27.0 1 computer False
3 100.0 1 toy False
5 200.0 1 book True
6 200.5 3 mouse True
7 202.0 3 door True
- Created the groups
g = df.groupby((df['asc_diff'].shift() != df['asc_diff']).cumsum())
for k, v in g:
print(f'[group {k}]')
print(v)
[group 1]
price weight product asc_diff
2 18.0 2 car True
1 20.0 2 apple True
0 22.0 1 banana True
[group 2]
price weight product asc_diff
4 27.0 1 computer False
3 100.0 1 toy False
[group 3]
price weight product asc_diff
5 200.0 1 book True
6 200.5 3 mouse True
7 202.0 3 door True
So far so good, but when I had to aggregate comes the problems:
def product_join(x):
return ' '.join(x)
g.agg({'weight': 'max', 'product': product_join})
weight product
asc_diff
1 2 car apple banana
2 1 computer toy
3 3 book mouse door
The problems:
- only group 1 and 3 should be aggregated (but in the code it applys to all groups)
- even using a custom function (e.g. product_join) I have no access to other columns values, so that I can get the weighted average prices for example.
What I want to accomplish:
- Aggregate only groups 1 and 3 (where
asc_diffis true) and keep group 2 intact - in
priceaggregate function I needed a function to access two columns (i.e.priceandweight) to get the weighted average value
Thanks in advance!