python - Groupby on condition and calculate sum of subgroups -
here data:
import numpy np import pandas pd z = pd.dataframe({'a':[1,1,1,2,2,3,3],'b':[3,4,5,6,7,8,9], 'c':[10,11,12,13,14,15,16]}) z b c 0 1 3 10 1 1 4 11 2 1 5 12 3 2 6 13 4 2 7 14 5 3 8 15 6 3 9 16 question:
how can calculation on different element of each subgroup? example, each group, want extract element in column 'c' corresponding element in column 'b' between 4 , 9, , sum them all.
here code wrote: (it runs cannot correct result)
gbz = z.groupby('a') # displaying groups: gbz.apply(lambda x: print(x)) list = [] def f(x): list_new = [] row in range(0,len(x)): if (x.iloc[row,0] > 4 , x.iloc[row,0] < 9): list_new.append(x.iloc[row,1]) list.append(sum(list_new)) results = gbz.apply(f) the output result should this:
c 0 1 12 1 2 27 2 3 15
it might easiest change order of operations, , filter against criteria first - not change after groupby.
z.query('4 < b < 9').groupby('a', as_index=false).c.sum() which yields
c 0 1 12 1 2 27 2 3 15
Comments
Post a Comment