Home > Software design >  Multiplying one column by another in pandas dataframe but getting a dtype('<U32') error
Multiplying one column by another in pandas dataframe but getting a dtype('<U32') error

Time:11-30

I have a dataframe where I am wanting to multiply each row in one column by the other (column Line Limit are data type float64) and return the sum of these.

enter image description here

def pol_stats(df,refcol,linecol,limitcol):
    """Summarise account file."""
    acc_count = len(pd.unique(df[refcol]))
    acc_exp = sum((df[linecol] / 100) * limitcol)
    return (acc_count,acc_exp)

pol_stats(df,'Reference','Line','Limit')

But I am getting the error 'ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')'.

I have tried using float() or to_numeric() on limitcol but still getting an error. Not sure why this would be an issue if both columns are data type float.

CodePudding user response:

Problem is instead column df[limitcol] was multiple by column name limitcol, so solution multiple by string, so raise error:

acc_exp = ((df[linecol] / 100) * df[limitcol]).sum()
  • Related