python count /sum records accross row with multiple condition [on hold]


python count /sum records accross row with multiple condition [on hold]



I current have a table which A,B,C represents product values while AA,BB,CC represents product type


A B C AA BB CC
10 20 0 1 2 0
5 0 15 1 2 3



i also have 2 arrays which are:


product =['A','B','C']
Product Value = ['AA','BB','CC']



My question is



1) To count/sum corresponding product type if its product value >0


product value >0


Product value Product Type(PT) count/sum same product if its value >0
------------- ---------------- ----------------------------------------------------
A B C AA BB CC product_1 product_2 product_1_sum product_2_sum
10 20 0 1 1 0 2 0 30 0
5 0 15 0 2 2 0 2 0 15



for first record, product A and B have value > 0 and A and B are product_1 category, so for product_1 its value is 2 count(AA BB), and its sum is 30 (10+20)


A


B


product_1


product_1



thx, pls help



Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





Pandas has excellant documentation
– wwii
Jul 1 at 2:01





Welcome to SO. Please take the time to read How to Ask and the other links found on that page.
– wwii
Jul 1 at 2:02






Sorry, I am a bit confused. How category1 and category2 are defined? category1 contains A and B and category2 C ? Or logic is somehing else?
– jezrael
Jul 1 at 4:44


category1


category2


category1


A


B


category2


C




1 Answer
1



Replicate your data


In [1]: values = [[10, 20, 0], [5, 0, 15]]
In [2]: types = [[1, 1, 0], [0, 2, 2]]



Possible types?


In [3]: possible_types = set.union(*[set(l) for l in types])
...: possible_types.discard(0)
...: possible_types = sorted(possible_types)



(the lines above see me indebted with this answer by vaultah, thank you!)



To have a nicely formatted output, we may want to count the input records


In [4]: from itertools import count



A loop over the records (rest d) and a first inner loop to count
items and sum values and a second inner loop, over possible_types,
to output the results


d


possible_types


In [5]: for line_no, vs, ts in zip(count(1), values, types):
...: print('Line no. %2.2d:'%line_no, end='')
...: d = {}
...: for v, t in zip(vs, ts):
...: d[t] = d.get(t, [0,0])
...: d[t][0] += 1
...: d[t][1] += v
...: for t in possible_types:
...: count, summ = d.get(t, [0, 0])
...: print('tProd.type %d, %2d item(s), sum of values is %3d '%
...: (t, count, summ), end='')
...: print()
Line no. 01: Prod.type 1, 2 item(s), sum of values is 30 Prod.type 2, 0 item(s), sum of values is 0
Line no. 02: Prod.type 1, 0 item(s), sum of values is 0 Prod.type 2, 2 item(s), sum of values is 15
In [6]:

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API