0

I have a dataframe where I'd like to add a column with conditional sum of values based on criteria in 2 (possibly 3) different columns. I'm trying to use lambda function such as:

df['newColumn'] = df[['colA','colB']].apply(lambda x,y: 
df.loc[df['colA']==x].loc[df['colB']==y]['Total Amount'].sum())

This approach doesn't work although when I test the .loc statement separately and use values in lieu of x and y I do get the correct sum. I would like to bring in another column to this if possible. The error Im getting is: "() missing 1 required positional argument: 'y'", 'occurred at index colA. Any help greatly appreciated,

whada
  • 306
  • 3
  • 15

1 Answers1

0

My guess is you want this:

df = pd.DataFrame({'A': [1,1,2,2,3,3],
                   'B': [2,2,2,3,3,3],
                   'TotalAmount': [10,20,30,40,50,60]})

df['NewColumn'] = df.groupby(['A', 'B'])['TotalAmount'].transform('sum')
df
#   A  B  TotalAmount  NewColumn
#0  1  2           10         30
#1  1  2           20         30
#2  2  2           30         30
#3  2  3           40         40
#4  3  3           50        110
#5  3  3           60        110
zipa
  • 27,316
  • 6
  • 40
  • 58