I have a dataframe that looks like this:
>>> df
value
time
2020-01-31 07:59:43.232 -6
2020-01-31 07:59:43.232 -2
2020-01-31 07:59:43.232 -1
2020-01-31 07:59:43.264 1
2020-01-31 07:59:43.389 0
2020-01-31 07:59:43.466 1
2020-01-31 07:59:43.466 5
2020-01-31 07:59:43.466 -1
2020-01-31 07:59:43.467 -1
2020-01-31 07:59:43.467 -1
2020-01-31 07:59:43.467 5
2020-01-31 07:59:43.467 1
I want to add 3 more columns that show the ratio of positive and negative values by a certain number. For example, if the number is 8:
value neg pos total
time
2020-01-31 07:59:43.232 -6
2020-01-31 07:59:43.232 -2 8 0 8
2020-01-31 07:59:43.232 -1
2020-01-31 07:59:43.264 1
2020-01-31 07:59:43.389 0
2020-01-31 07:59:43.466 1
2020-01-31 07:59:43.466 5 1 7 8
2020-01-31 07:59:43.466 -1
2020-01-31 07:59:43.467 -1
2020-01-31 07:59:43.467 -1
2020-01-31 07:59:43.467 5 3 5 8
2020-01-31 07:59:43.467 1
If the number is 5:
value neg pos total
time
2020-01-31 07:59:43.232 -6 5 0 5 # take just 5 out of -6 and the rest(-1) is used for the next calculation
2020-01-31 07:59:43.232 -2
2020-01-31 07:59:43.232 -1
2020-01-31 07:59:43.264 1 4 1 5 # sum(abs(list(-1, -2, -1, 1)))
2020-01-31 07:59:43.389 0
2020-01-31 07:59:43.466 1
2020-01-31 07:59:43.466 5 0 5 5 # 1 + 5 -> take just 5(1, 4) out of them and the rest(1) is used for the next calculation
2020-01-31 07:59:43.466 -1
2020-01-31 07:59:43.467 -1
2020-01-31 07:59:43.467 -1
2020-01-31 07:59:43.467 5 3 4 5 # 1, -1, -1, -1, 5 -> take just 5(1, -1, -1, -1, 1) out of them and the rest(4) is used for the next calculation
2020-01-31 07:59:43.467 1 0 5 5 # 4, 1
I've been doing the calculation with a loop and several conditional statements and it's pretty slow. I wonder if there are more efficient and faster ways to do this.
THE CODE BELOW SHOWS HOW I'VE DONE WHEN THE NUMBER IS 300(GROUP_SIZE)
GROUP_SIZE = 300
for DATE in lst_requiredDates:
df = dic_dtf[DATE]
lst_groups = []
lst_group = [0, 0, 0, 0]
for index, row in df.iterrows():
date = index
value = row['value']
abs_value = abs(value)
if (lst_group[3]+abs_value) < GROUP_SIZE:
if value < 0:
lst_group[0] = date
lst_group[1] += abs_value
lst_group[3] += abs_value
else:
lst_group[0] = date
lst_group[2] += abs_value
lst_group[3] += abs_value
elif (lst_group[3]+abs_value) == GROUP_SIZE:
if value < 0:
lst_group[0] = date
lst_group[1] += abs_value
lst_group[3] += abs_value
else:
lst_group[0] = date
lst_group[2] += abs_value
lst_group[3] += abs_value
lst_groups.append(lst_group)
lst_group = [0, 0, 0, 0]
elif (lst_group[3]+abs_value) > GROUP_SIZE:
int_left = (lst_group[3]+abs_value) - GROUP_SIZE
if value < 0:
lst_group[0] = date
lst_group[1] += (abs_value - int_left)
lst_group[3] += (abs_value - int_left)
lst_groups.append(lst_group)
lst_group = [0, 0, 0, 0]
lst_group[0] = date
lst_group[1] += int_left
lst_group[3] += int_left
else:
lst_group[0] = date
lst_group[2] += (abs_value - int_left)
lst_group[3] += (abs_value - int_left)
lst_groups.append(lst_group)
lst_group = [0, 0, 0, 0]
lst_group[0] = date
lst_group[2] += int_left
lst_group[3] += int_left