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

Pandas: how to multiply columns that only contain values

I have a small script that reads a csv file and maps multiplier calculations to the corresponding object and column, the map function works perfectly well as can be seen in the image attached.

I would like to then only multiply columns that contain a value to a ‘total multiplier’ column and ignore all cells that contain 0 values.

multipliers = {
        'Object 1': factor1,
        'Object 2': factor2,
        'Object 3': factor3,
               
}

    df['A'] = df['Param1'].map(multipliers)
    df['B'] = df['Param2'].map(multipliers)
    df['C'] = df['Param3'].map(multipliers)
    
    #total multiplier is A*B*C:
        
    df['Total Multiplier'] = #help needed here

This is the CSV with the column showing what it should output – as you can see some cells have 0 values so when mutliplied the output is 0 which is wrong for me.

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

enter image description here

>Solution :

I think a simple way to do it could be to fill empty cells with 1 using fillna. Something like:

df_ = df.fillna(1)
df['Total Multiplier'] = df_['A'] * df_['B'] * df_['C']
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