【问题标题】:Combinations of all dataframe columns in pythonpython中所有数据框列的组合
【发布时间】:2020-12-16 12:23:04
【问题描述】:

我有三个具有相同索引(国家/地区)的数据框。 我需要找到三个数据框的所有组合,用数据框创建新列。在每一列下,我将得到这些组合的值的乘积。

Envelope = pd.read_excel("Envelope.xlsx",index_col=0)
Shading = pd.read_excel("Shading.xlsx",index_col=0)
ThermalMass = pd.read_excel("ThermalMass.xlsx",index_col=0)
#Envelope dataframe
Country         Group(A)  Group(B)  Group(C)                       
France          0.4       0.4       0.2
Brussels        0.8       0.1       0.1
Germany_A       0.3       0.6       0.1
Germany_B       0.2       0.5       0.3

#Shading dataframe            
Country     YeSH  NoSH        
France      0.5   0.5
Brussels    0.6   0.4
Germany_A   0.9   0.1
Germany_B   0.4   0.6

#ThermalMass dataframe             
Country     Heavy   Light         
France       0.4    0.6
Brussels     0.5    0.5
Germany_A    0.3    0.7
Germany_B    0.5    0.5`

我尝试使用 MultiIndex.from_product

all = pd.MultiIndex.from_product([Envelope,Shading,ThermalMass])

但结果仅适用于标题:

print(all)
MultiIndex([('Group(A)', 'YeSH', 'Heavy'),
            ('Group(A)', 'YeSH', 'Light'),
            ('Group(A)', 'NoSH', 'Heavy'),
            ('Group(A)', 'NoSH', 'Light'),
            ('Group(B)', 'YeSH', 'Heavy'),
            ('Group(B)', 'YeSH', 'Light'),
            ('Group(B)', 'NoSH', 'Heavy'),
            ('Group(B)', 'NoSH', 'Light'),
            ('Group(C)', 'YeSH', 'Heavy'),
            ('Group(C)', 'YeSH', 'Light'),
            ('Group(C)', 'NoSH', 'Heavy'),
            ('Group(C)', 'NoSH', 'Light')],
           )

我需要每个国家/地区的值,所以它应该看起来像这样 (3 x 2x 2) = 12 个组合:

           Group(A)_YeSH_Heavy  Group(A)_YeSH_Light  Group(A)_NoSH_Heavy   Group(A)_NoSH_Light
Country                 
France       0.08                0.12                 0.08                    0.12 
Brussels     0.24                0.24                 0.16                    0.16
Germany_A    0.081               0.189                0.009                   0.6
Germany_B    0.04                 0.04                0.06                    0.06

如何创建新列和三个数据框的组合?

【问题讨论】:

标签: python dataframe combinations


【解决方案1】:

您可以执行以下操作:

from itertools import product

# Only if country isn't the index yet
Envelope.set_index('Country', drop=True, inplace=True)
Shading.set_index('Country', drop=True, inplace=True)
ThermalMass.set_index('Country', drop=True, inplace=True)

columns = list(product(Envelope.columns, Shading.columns, ThermalMass.columns))
df = pd.concat([Envelope[col[0]] * Shading[col[1]] * ThermalMass[col[2]]
                for col in columns],
               axis='columns')
df.columns = ['_'.join(c for c in col) for col in columns]

输出:

           Group(A)_YeSH_Heavy  ...  Group(C)_NoSH_Light
Country                         ...                     
France                   0.080  ...                0.060
Brussels                 0.240  ...                0.020
Germany_A                0.081  ...                0.007
Germany_B                0.040  ...                0.090

[4 rows x 12 columns]

【讨论】:

    【解决方案2】:

    改编自this answer,这是一个使用MultiIndex的矢量化方法。

    pidx = np.indices((Envelope.shape[1], Shading.shape[1], ThermalMass.shape[1])).reshape(3, -1)
    lcol = pd.MultiIndex.from_product([Envelope, Shading, ThermalMass])
    pd.DataFrame(Envelope.values[:, pidx[0]] * Shading.values[:, pidx[1]] * ThermalMass.values[:, pidx[2]],
                columns=lcol, index=Envelope.index)
    

    给予:

              Group(A)                      Group(B)                       \
                  YeSH          NoSH            YeSH          NoSH          
                 Heavy  Light  Heavy  Light    Heavy  Light  Heavy  Light   
    Country                                                                 
    France       0.080  0.120  0.080  0.120    0.080  0.120  0.080  0.120   
    Brussels     0.240  0.240  0.160  0.160    0.030  0.030  0.020  0.020   
    Germany_A    0.081  0.189  0.009  0.021    0.162  0.378  0.018  0.042   
    Germany_B    0.040  0.040  0.060  0.060    0.100  0.100  0.150  0.150   
    
              Group(C)                       
                  YeSH          NoSH         
                 Heavy  Light  Heavy  Light  
    Country                                  
    France       0.040  0.060  0.040  0.060  
    Brussels     0.030  0.030  0.020  0.020  
    Germany_A    0.027  0.063  0.003  0.007  
    Germany_B    0.060  0.060  0.090  0.090  
    

    【讨论】:

      猜你喜欢
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多