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

How to multiply two columns of a Dataframe?

Good afternoon, I am trying to multiply two columns of a dataframe (C). And add the results to a new column. I have tried different methods but none work. The most common error I encounter is:

TypeError: can't multiply sequence by non-int of type 'float'

The columns that a i want to multiply are: H04_PEDRO_MARIN and SS(mg/l). And also i want to create a new column with the results.

C:
                H04_PEDRO_MARIN SS(mg/l)  multiplication
Fecha
26/07/11 14:00         0.000000     80.4        0.000000
26/07/11 15:00         0.000000     76.1        0.000000
26/07/11 16:00         0.000000        0        0.000000
26/07/11 17:00         0.000000        0        0.000000
26/07/11 18:00         0.000000        0        0.000000
...                         ...      ...             ...
12/04/12 10:00      9430.166667    61.18     9430.166667
12/04/12 11:00      9430.166667    60.05     9430.166667
12/04/12 12:00      9430.166667    59.43     9430.166667
12/04/12 14:00      9430.166667    56.98     9430.166667

[11568 rows x 3 columns]

I have tried:

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

C['multiplicaction'] = C['H04_PEDRO_MARIN'][1])*(C['SS(mg/l)'])

And

cols = ['H04_PEDRO_MARIN','SS(mg/l)']
C['multiplication'] = C[cols].prod(axis=1)

And don´t work

Even i have tried to separate both columns in different dataframes and multiply and don’t work again.

Thanks for any solution.

>Solution :

You can do this:

import pandas as pd

data = {
    'H04_PEDRO_MARIN': [0.0, 0.0, 0.0, 0.0, 0.0, 9430.166667, 9430.166667, 9430.166667, 9430.166667],
    'SS(mg/l)': [80.4, 76.1, 0.0, 0.0, 0.0, 61.18, 60.05, 59.43, 56.98]
}
C = pd.DataFrame(data, index=['26/07/11 14:00', '26/07/11 15:00', '26/07/11 16:00', '26/07/11 17:00', '26/07/11 18:00', '12/04/12 10:00', '12/04/12 11:00', '12/04/12 12:00', '12/04/12 14:00'])

C['multiplication'] = C['H04_PEDRO_MARIN'] * C['SS(mg/l)']

print(C)

which givs

                H04_PEDRO_MARIN  SS(mg/l)  multiplication
26/07/11 14:00         0.000000     80.40        0.000000
26/07/11 15:00         0.000000     76.10        0.000000
26/07/11 16:00         0.000000      0.00        0.000000
26/07/11 17:00         0.000000      0.00        0.000000
26/07/11 18:00         0.000000      0.00        0.000000
12/04/12 10:00      9430.166667     61.18   576937.596687
12/04/12 11:00      9430.166667     60.05   566281.508353
12/04/12 12:00      9430.166667     59.43   560434.805020
12/04/12 14:00      9430.166667     56.98   537330.896686

if you have trouble with datatypes, change to

C['H04_PEDRO_MARIN'] = pd.to_numeric(C['H04_PEDRO_MARIN'], errors='coerce')
C['SS(mg/l)'] = pd.to_numeric(C['SS(mg/l)'], errors='coerce')
C['multiplication'] = C['H04_PEDRO_MARIN'] * C['SS(mg/l)']

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