I have these dictionaries and want to get the difference between the min,max value for same product.
{'time': '2021-11-25T09:27:29.969484Z', 'product_symbol': 'ABCD', 'price': -5.9887999999999995, 'team': 'Csk'}
{'time': '2021-11-25T09:28:11.371546Z', 'product_symbol': 'ABCD', 'price': -3.0239, 'team': 'Mi'}
{'time': '2021-11-25T09:29:29.854840Z', 'product_symbol': 'ABCD', 'price': -8.273820066171476, 'team': 'rcb'}
{'time': '2021-11-25T09:30:35.803968Z', 'product_symbol': 'ABCD', 'price': -6.96, 'team': 'dc'}
{'time': '2021-11-25T09:31:28.967498Z', 'product_symbol': 'ABCD', 'price': -5.9887999999999995, 'team': 'csk'}
{'time': '2021-11-25T09:32:12.905123Z', 'product_symbol': 'XYZ', 'price': -3.0239, 'team': 'Mi'}
{'time': '2021-11-25T09:27:05.320127Z', 'product_symbol': 'XYZ', 'price': 4.3959, 'team': 'Mi'}
{'time': '2021-11-25T09:27:52.602959Z', 'product_symbol': 'XYZ', 'price': 1.5423, 'team': 'Dc'}
{'time': '2021-11-25T09:28:36.802078Z', 'product_symbol': 'XYZ', 'price': 6.514124954262218, 'team': 'csk'}
{'time': '2021-11-25T09:30:05.360497Z', 'product_symbol': 'XYZ', 'price': 1.76, 'team': 'Mi'}
{'time': '2021-11-25T09:31:04.167415Z', 'product_symbol': 'XYZ', 'price': 4.3959, 'team': 'Csk'}
{'time': '2021-11-25T09:31:53.907419Z', 'product_symbol': 'XYZ', 'price': 1.5423, 'team': 'Mi'}
for every product given with product symbol, need to take minimum and maximum prices and the difference is printed.
Example:
product_symbol : ABCD
minimum : -8.27
maximum : -3.02
Differance : -5.23
CodePudding user response:
Here is a way using pandas:
importing the data
l = [{'time': '2021-11-25T09:27:29.969484Z', 'product_symbol': 'ABCD', 'price': -5.9887999999999995, 'team': 'Csk'},
{'time': '2021-11-25T09:28:11.371546Z', 'product_symbol': 'ABCD', 'price': -3.0239, 'team': 'Mi'},
{'time': '2021-11-25T09:29:29.854840Z', 'product_symbol': 'ABCD', 'price': -8.273820066171476, 'team': 'rcb'},
{'time': '2021-11-25T09:30:35.803968Z', 'product_symbol': 'ABCD', 'price': -6.96, 'team': 'dc'},
{'time': '2021-11-25T09:31:28.967498Z', 'product_symbol': 'ABCD', 'price': -5.9887999999999995, 'team': 'csk'},
{'time': '2021-11-25T09:32:12.905123Z', 'product_symbol': 'XYZ', 'price': -3.0239, 'team': 'Mi'},
{'time': '2021-11-25T09:27:05.320127Z', 'product_symbol': 'XYZ', 'price': 4.3959, 'team': 'Mi'},
{'time': '2021-11-25T09:27:52.602959Z', 'product_symbol': 'XYZ', 'price': 1.5423, 'team': 'Dc'},
{'time': '2021-11-25T09:28:36.802078Z', 'product_symbol': 'XYZ', 'price': 6.514124954262218, 'team': 'csk'},
{'time': '2021-11-25T09:30:05.360497Z', 'product_symbol': 'XYZ', 'price': 1.76, 'team': 'Mi'},
{'time': '2021-11-25T09:31:04.167415Z', 'product_symbol': 'XYZ', 'price': 4.3959, 'team': 'Csk'},
{'time': '2021-11-25T09:31:53.907419Z', 'product_symbol': 'XYZ', 'price': 1.5423, 'team': 'Mi'},
]
df = pd.DataFrame(l)
processing
df2 = (df.groupby('product_symbol')['price'].agg(minimum='min', maximum='max')
.eval('difference = maximum-minimum')
# or, instead of eval:
#.assign(difference=lambda d:d['maximum']-d['minimum'])
)
output and conversion to dictionary:
>>> df2
minimum maximum difference
product_symbol
ABCD -8.27382 -3.023900 5.249920
XYZ -3.02390 6.514125 9.538025
>>> df2.T.to_dict()
{'ABCD': {'minimum': -8.273820066171476,
'maximum': -3.0239,
'difference': 5.249920066171477},
'XYZ': {'minimum': -3.0239,
'maximum': 6.514124954262218,
'difference': 9.538024954262218},
}