I have a dataframe with dates and origins and amount, now I need to find out, for the same date, the corresponding sum in origin "s" and two amounts in origin "c" summing up to get the corresponding sum in "s", if it is not clear, here is an example :
df = pd.DataFrame( {"amounts": [12, 13, 1, 14, 2, 15, 25, 29, 45],
"date": ['2022/11/11', '2022/11/11', '2022/11/11', '2022/11/12','2022/11/12', '2022/11/12',
'2022/11/11', '2022/11/12', '2022/11/12'],
"origin": ['C', 'C', 'C', 'C','C', 'S', 'S', 'S']})
so i need to get an output precizing 12, 13, and 25 are one group and 14, 15 and 29 another group, identified by a new column with letters A, B, C, D... or other.
Can anyone give some idea please?
I tried to group by fistly the df by date and origin, and this function, but i m not sure if there is better solution, because this function do not identify numbers with a new column:
def check_sum_exists(nums, target_sum):
for i in range(len(nums)):
# one number sum
if nums[i] == target_sum:
return True
# two numbers sum
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target_sum:
return True
# three numbers sum
for k in range(i+2, len(nums)):
if nums[i] + nums[j]+ nums[k] == target_sum:
return True
return False