Home > database >  Pandas: how to multiply columns that only contain values
Pandas: how to multiply columns that only contain values

Time:06-09

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.

enter image description here

CodePudding user response:

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']
  • Related