Search code examples
pythonpandasdataframesumcombinations

Sum all combinations of 2 columns from 2 dataframes


I have 2 dataframes df1 and df2 (same index and number of rows), and I would like to create a new dataframe which columns are the sum of all combinations of 2 columns from df1 and df2, example :

input :

import pandas as pd
df1 =  pd.DataFrame([[10,20]])
df2 =  pd.DataFrame([[1,2]])

output :

import pandas as pd
df3 =  pd.DataFrame([[11,12,21,22]])

Solution

  • Use MultiIndex.from_product for all combinations and sum DataFrames with repeated values by DataFrame.reindex:

    mux = pd.MultiIndex.from_product([df1.columns, df2.columns])
    
    df = df1.reindex(mux, level=0, axis=1) + df2.reindex(mux, level=1, axis=1)
    df.columns = range(len(df.columns))