I have three data frames that have the same index (Countries). I need to find all the combinations of the three data frames, create new columns with the data frames. Under each of those columns I will have the multiplication of the values from those combinations.
JavaScript
x
4
1
Envelope = pd.read_excel("Envelope.xlsx",index_col=0)
2
Shading = pd.read_excel("Shading.xlsx",index_col=0)
3
ThermalMass = pd.read_excel("ThermalMass.xlsx",index_col=0)
4
JavaScript
1
21
21
1
#Envelope dataframe
2
Country Group(A) Group(B) Group(C)
3
France 0.4 0.4 0.2
4
Brussels 0.8 0.1 0.1
5
Germany_A 0.3 0.6 0.1
6
Germany_B 0.2 0.5 0.3
7
8
#Shading dataframe
9
Country YeSH NoSH
10
France 0.5 0.5
11
Brussels 0.6 0.4
12
Germany_A 0.9 0.1
13
Germany_B 0.4 0.6
14
15
#ThermalMass dataframe
16
Country Heavy Light
17
France 0.4 0.6
18
Brussels 0.5 0.5
19
Germany_A 0.3 0.7
20
Germany_B 0.5 0.5`
21
I tried to use the MultiIndex.from_product
JavaScript
1
2
1
all = pd.MultiIndex.from_product([Envelope,Shading,ThermalMass])
2
but the results is only for the titles:
JavaScript
1
15
15
1
print(all)
2
MultiIndex([('Group(A)', 'YeSH', 'Heavy'),
3
('Group(A)', 'YeSH', 'Light'),
4
('Group(A)', 'NoSH', 'Heavy'),
5
('Group(A)', 'NoSH', 'Light'),
6
('Group(B)', 'YeSH', 'Heavy'),
7
('Group(B)', 'YeSH', 'Light'),
8
('Group(B)', 'NoSH', 'Heavy'),
9
('Group(B)', 'NoSH', 'Light'),
10
('Group(C)', 'YeSH', 'Heavy'),
11
('Group(C)', 'YeSH', 'Light'),
12
('Group(C)', 'NoSH', 'Heavy'),
13
('Group(C)', 'NoSH', 'Light')],
14
)
15
I need to have the values for each countries, so it should look like this (3 x 2x 2) = 12 combinations:
JavaScript
1
7
1
Group(A)_YeSH_Heavy Group(A)_YeSH_Light Group(A)_NoSH_Heavy Group(A)_NoSH_Light
2
Country
3
France 0.08 0.12 0.08 0.12
4
Brussels 0.24 0.24 0.16 0.16
5
Germany_A 0.081 0.189 0.009 0.6
6
Germany_B 0.04 0.04 0.06 0.06
7
How can I create the new columns and the combinations of the three dataframes?
Advertisement
Answer
You could do the following:
JavaScript
1
13
13
1
from itertools import product
2
3
# Only if country isn't the index yet
4
Envelope.set_index('Country', drop=True, inplace=True)
5
Shading.set_index('Country', drop=True, inplace=True)
6
ThermalMass.set_index('Country', drop=True, inplace=True)
7
8
columns = list(product(Envelope.columns, Shading.columns, ThermalMass.columns))
9
df = pd.concat([Envelope[col[0]] * Shading[col[1]] * ThermalMass[col[2]]
10
for col in columns],
11
axis='columns')
12
df.columns = ['_'.join(c for c in col) for col in columns]
13
Output:
JavaScript
1
9
1
Group(A)_YeSH_Heavy Group(C)_NoSH_Light
2
Country
3
France 0.080 0.060
4
Brussels 0.240 0.020
5
Germany_A 0.081 0.007
6
Germany_B 0.040 0.090
7
8
[4 rows x 12 columns]
9