Python Pandas Dataframe Multiplying 2 columns results replicated values of column A

Question:

OK I am trying something I think should be very simple but it’s doesn’t seem to be working.

I have a Panda Dataframe which includes many columns. There are 2 which I want to multiply but instead of the resulting new column displaying the result of multiplying the two values, it shows the value in column A the number of times that is in column B

Example:

dataframe['totalcost'] = dataframe['orderedQuantity.amount'].mul(dataframe['netCost.amount'])

Also tried this:

dataframe['totalcost'] = dataframe['orderedQuantity.amount'] * dataframe['netCost.amount']

netCost.amount = 12

orderedQuantity.amount = 3

totalcost results in 121212 instead of 36

What am I missing?

Asked By: Reg

||

Answers:

Seems like the values in orderedQuantity are string/object type. Cast to numeric to resolve the issue

dataframe['totalcost'] = dataframe['orderedQuantity.amount'].mul(dataframe['netCost.amount'].astype(float))
Answered By: Shubham Sharma
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.