Home > front end >  SUMPRODUCT two columns PYTHON
SUMPRODUCT two columns PYTHON

Time:10-05

I am working on clipping loss coding.. I want an example code of how to SUMPRODUCT two columns

clipping = clipping_daily ['Column1'] * [int(x) for x in 'Column2'].sum(axis=1)

CodePudding user response:

I guess you meant:

clipping = clipping_daily['Column1'] * clipping_daily['Column2'].astype(int)

Or you mean by?

clipping = clipping_daily['Column1'] * clipping_daily['Column2'].astype(int).sum()

CodePudding user response:

I think you better use groupby:

clipping  = df.groupby(['Column1', 'Column2']).sum()

CodePudding user response:

EDIT:

With some inspiration from U12-Forward answer to make it all pandas-operations

This should do it (if I understand your problem correctly)

sum_col_2 = clipping_daily['Column2'].astype(int).sum()
clipping = clipping_daily ['Column1'] * sum_col_2

or as one -liner

clipping = clipping_daily ['Column1'] * clipping_daily['Column2'].astype(int).sum()
  • Related