Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

dataframe['totalcost'] = dataframe['orderedQuantity.amount'].astype(float).mul(dataframe['netCost.amount'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading