Home > Enterprise >  Sum all combinations of 2 columns from 2 dataframes
Sum all combinations of 2 columns from 2 dataframes

Time:01-21

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]])

CodePudding user response:

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))

CodePudding user response:

IIUC you can do this with numpy.

>>> import numpy as np
>>> n = df1.shape[1]
>>> pd.DataFrame(df1.values.repeat(n)   np.tile(df2.values, n))
    0   1   2   3
0  11  12  21  22
  •  Tags:  
  • Related