I am new to Pandas and trying to figure out how to create/use functions, perform simple if-else statements. I wrote the following function average_shares(), but it doesn't work as i expect (cause it should be written for rows, not columns, but i don't get it/ don't know how to fix the following code. In a simple non-pandas world I would write for i in range(len(column)): do the following. In pandas tutorials they didn't use for loops, they used apply(); they created functions with a parameter row (why not dataframe df as parameter?). I tried to do the same and it didn't work. So overall, I just don't know how to do a simple thing in pandas and I hoped maybe someone could show me. df['share'] = df['order'] / df.groupby(['week', 'id'])['order'].transform('sum') df['index'] = df.groupby(['week', 'id']).cumcount() + 1 df['repeats'] = df.groupby(['week', 'id'])['order'].transform('count') def average_shares(df): if df['repeats']>2: df = df[(df['share']) & (df['share']<0.97)] df['share_1'] = df[df['index']==1].groupby('id')['share'].transform('mean') df['share_2'] = df[df['index']==2].groupby('id')['share'].transform('mean') elif df['repeats']==2: df['share_1'] = df[df['index']==1].groupby('id')['share'].transform('mean') df['share_2'] = df[df['index']==2].groupby('id')['share'].transform('mean') elif df['repeats']<2: df['share_1'] = df['share'] df['share_2'] = 0 return df['share_1'], df['share_2'] df['share_1'], df['share_2'] = zip(*df.apply(average_shares, axis=1)) The task sounds as In the dataframe there are columns: id, week, order, share. Each week can be repeated more than once. For each id, share_1 is an average share of the first occurrences of weeks, and share_2 is an average share of the second occurrences. If for some id a week is repeated more than 2 times, then before averaging, save the rows with a share >0.97 and <0.03 are discarded. If some week is not repeated, then the average share of the first occurrence is equal to the share itself, the average share of the second occurrence is 0. Here is the data: |input |calculated |I need to calculate | |-------------|-----------------|---------------- ---------------| |id|week|order|index|share |len|average share_1 |average share_2| |--|----|-----|-----|-------|---|----------------|---------------| |1 | 1 |2 |1 |2/(2+4)|2 |(2/6+1)/2 = 0.66|(4/6+0)/2 =0.33| |1 | 1 |4 |2 |4/(2+4)|2 |(2/6+1)/2 = 0.66|(4/6+0)/2 =0.33| |1 | 2 |3 |1 |1 |1 |(2/6+1)/2 = 0.66|(4/6+0)/2 =0.33| len = number of repeats of the same week index = order of apperance of the repeats share = order devided by the sum of the week orders share_1 = average share among weeks with index 1 (with the same id) share_2 = average share among weeks with index 2 (with the same id) Here is a picture that explains what im doing: image Continue reading...