从两个数据框创建交互条件

时间:2019-04-15 08:13:49

标签: python pandas

我有两个对齐的虚拟变量数据框。我想将两者相乘并获得一个新的数据框结果,即两者与3行和6列(ItalyxJan,ItalyxFeb,ItalyxMar,ChinaxJan ..)的相互作用。

# Creating the first dataframe  
df1=pd.DataFrame({"Italy":[0,0,1], 
                  "China":[1,1,0]}) 

# Creating the second dataframe with <code>Na</code> value 
df2=pd.DataFrame({"Jan":[1,0,0], 
                  "Feb":[0,1,0], 
                  "Mar":[0,0,1]}) 

df3 = df1.mul(df2.values, axis=0)

,但是我收到了错误消息

ValueError: Unable to coerce to DataFrame, shape must be (3, 2): given (3, 3)


##Expected outputs

df3=pd.DataFrame({"Italy*Jan":[0,0,0], 
                  "Italy*Feb":[0,0,0], 
                  "Italy*Mar":[0,0,1],
                  "China*Jan":[1,0,1],
                  "China*fe":[0,1,0],
                  "Chian*Mar":[0,0,0]}) 

建议?

1 个答案:

答案 0 :(得分:3)

您可以同时创建MultiIndex.from_productDataFrame.reindex,因此可能会多个:

mux = pd.MultiIndex.from_product([df1.columns, df2.columns])

df1 = df1.reindex(mux, axis=1, level=0)
print (df1)
  Italy         China        
    Jan Feb Mar   Jan Feb Mar
0     0   0   0     1   1   1
1     0   0   0     1   1   1
2     1   1   1     0   0   0

df2 = df2.reindex(mux, axis=1, level=1)
print (df2)
  Italy         China        
    Jan Feb Mar   Jan Feb Mar
0     1   0   0     1   0   0
1     0   1   0     0   1   0
2     0   1   1     0   1   1

df3 = df1.mul(df2, axis=0)
print (df3)
  Italy         China        
    Jan Feb Mar   Jan Feb Mar
0     0   0   0     1   0   0
1     0   0   0     0   1   0
2     0   1   1     0   0   0

最后可以用MultiIndexmap展平join

df3.columns = df3.columns.map('x'.join)
print (df3)
   ItalyxJan  ItalyxFeb  ItalyxMar  ChinaxJan  ChinaxFeb  ChinaxMar
0          0          0          0          1          0          0
1          0          0          0          0          1          0
2          0          1          1          0          0          0