如何根据列和索引将两个数据框相乘?

时间:2018-10-02 15:17:37

标签: python pandas multiplication

我有以下两个数据框:

date  = ['2015-02-03 23:00:00','2015-02-03 23:30:00','2015-02-04 00:00:00','2015-02-04 00:30:00','2015-02-04 01:00:00','2015-02-04 01:30:00','2015-02-04 02:00:00','2015-02-04 02:30:00','2015-02-04 03:00:00','2015-02-04 03:30:00','2015-02-04 04:00:00','2015-02-04 04:30:00','2015-02-04 05:00:00','2015-02-04 05:30:00','2015-02-04 06:00:00','2015-02-04 06:30:00','2015-02-04 07:00:00','2015-02-04 07:30:00','2015-02-04 08:00:00','2015-02-04 08:30:00','2015-02-04 09:00:00','2015-02-04 09:30:00','2015-02-04 10:00:00','2015-02-04 10:30:00','2015-02-04 11:00:00','2015-02-04 11:30:00','2015-02-04 12:00:00','2015-02-04 12:30:00','2015-02-04 13:00:00','2015-02-04 13:30:00','2015-02-04 14:00:00','2015-02-04 14:30:00','2015-02-04 15:00:00','2015-02-04 15:30:00','2015-02-04 16:00:00','2015-02-04 16:30:00','2015-02-04 17:00:00','2015-02-04 17:30:00','2015-02-04 18:00:00','2015-02-04 18:30:00','2015-02-04 19:00:00','2015-02-04 19:30:00','2015-02-04 20:00:00','2015-02-04 20:30:00','2015-02-04 21:00:00','2015-02-04 21:30:00','2015-02-04 22:00:00','2015-02-04 22:30:00','2015-02-04 23:00:00','2015-02-04 23:30:00']
value = [33.24  , 31.71  , 34.39  , 34.49  , 34.67  , 34.46  , 34.59  , 34.83  , 35.78  , 33.03  , 35.49  , 33.79  , 36.12  , 37.09  , 39.54  , 41.19  , 45.99  , 50.23  , 46.72  , 47.47  , 48.46  , 48.38  , 48.40  , 48.13  , 38.35  , 38.19  , 38.12  , 38.05  , 38.06  , 37.83  , 37.49  , 37.41 , 41.84  , 42.26 , 44.09  , 48.85  , 50.07 , 50.94  , 51.09  , 50.60  , 47.39  , 45.57  , 45.03  , 44.98  , 41.32  , 40.37  , 41.12  , 39.33  , 35.38  , 33.44  ]
value2 = [2*x for x in value]
value3 = [3*x for x in value]
df = pd.DataFrame({'value':value,'value2':value2,'value3':value3,'index':date})
df.index = pd.to_datetime(df['index'],format='%Y-%m-%d %H:%M')
df.drop(['index'],axis=1,inplace=True)

print(df.head())
                    value  value2  value3
index                                     
2015-02-03 23:00:00  33.24   66.48   99.72
2015-02-03 23:30:00  31.71   63.42   95.13
2015-02-04 00:00:00  34.39   68.78  103.17
2015-02-04 00:30:00  34.49   68.98  103.47
2015-02-04 01:00:00  34.67   69.34  104.01


value4 = [4*x for x in value]
value5 = [5*x for x in value]
df2 = pd.DataFrame({'value':value,'value2':value4,'value3':value5,'index':date})
df2.index = pd.to_datetime(df2['index'],format='%Y-%m-%d %H:%M')
df2.drop(['index'],axis=1,inplace=True)
print(df2.head())

                     value  value2  value3
index                                     
2015-02-03 23:00:00  33.24  132.96  166.20
2015-02-03 23:30:00  31.71  126.84  158.55
2015-02-04 00:00:00  34.39  137.56  171.95
2015-02-04 00:30:00  34.49  137.96  172.45
2015-02-04 01:00:00  34.67  138.68  173.35

我想通过以下方式将两个数据帧有效地相乘:

  • 两个数据框可能没有相同的大小,列和行的数量也不同,也没有相同的值
  • 我想获得一个数据框,当列具有相同的命名并且两个数据框中的索引相同时,这将是两个数据框的乘积。
  • 例如,我想将df1的“值”列“ 2015-02-03 23:00:00”中的值33.24与列的“值”列'2015-02-03 23中的值相乘df2的:00:00'。我想对两个数据框中的所有列和行执行相同的操作

关于如何做到这一点的任何想法?

预期结果将是基于列和索引的两个数据框“内部”相乘的数据框。

非常感谢您的帮助,

1 个答案:

答案 0 :(得分:0)

例如,您具有以下数据框

df1=pd.DataFrame({'A':[1,2],'B':[2,3]},index=[0,2])


df2=pd.DataFrame({'A':[1,2],'C':[2,3]},index=[0,1])

使用mul后跟dropna

df1.mul(df2).dropna(thresh=1).dropna(1)# may not need the dropna(1)
Out[651]: 
     A
0  1.0