Search code examples
pythonpandasdataframesummultiplication

multiply and sum two columns in two dataframes in Python


I have two dataframes both with 6 rows. I want to multiply the values in two selected columns from the two dataframes (one from each df)

result = sum(a * b for a, b in zip(list(df1['col1']), list(df2['col3'])))

I do not seem to get what I want. I did the calc "manually" in Excel (for one date in my time series), which gave me the expected result. So my question is if I did something wrong?


Solution

  • You will can just use pandas abstractions for it.

    result = df['col1'] * df['col3']
    

    If then you want to get the sum of those result values you can just do:

    sum(results)