I have a following data frame in pandas
order_id quant_bought
0 519 3
2 520 3
5 521 1
6 523 6
11 524 1
12 525 1
13 526 3
16 527 1
17 528 1
18 529 1
19 530 1
20 531 3
22 532 1
23 533 3
26 534 1
27 535 3
What I am trying to do is, find all the quant_bought values equals to sum 6. I have one variable which has dynamic value which, I get after some calculation.
required = 6
Now I am generating 6 dynamic list based on above variable value
required = []
for i in range(required):
required.append([])
Above code will generate 6 dynamic lists.
Now I want to fill this list with all quant_bought
elements which sums to 6
It should look like this.
required1 = [1,1,1,1,1,1]
required2 = [1,1,1,3]
required3 = [3,3]
required4 = [3,3]
required5 = [6]
required6 = [3] ## Which is left out
And corresponding index so that I can subset on dataframe. I am doing following in python
## Sorting the dataframe in ascending order
tsp_data_unique_sorted = tsp_data_unique.sort(['quant_bought'], ascending=
[True])
def tsum(currentSum,total,input,record,n):
if total==sum :
for i in range(0,n):
if record[i]:
print input[i]
i = i+1
for i in range(i,n):
if record[i]:
print input[i]
print ""
return
i = currentSum
for i in range(i,n):
if total + input[i] > sum :
continue
if i > 0 and input[i] == input[i-1] and not record[i-1] :
continue
record[i] = 1
tsum(i + 1,total + input[i],input,record,l)
record[i] = 0
record = []
sum = 6
input = tsp_data_unique_sorted['quant_bought'].values.tolist()
l = len(input)
for i in range(0,l):
record.append(0)
print "From the array the elements equal to 6 are:"
Which gives me following output.
tsum(0,0,input,record,l)
1
1
1
1
1
1
1
1
1
3
3
3
6
But, it did not take 3 elements into consideration (3,3,3). I want to store these into dynamic list which I have created. Please help.