0

I have the following two dataframes:

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

I want to efficiently multiply both dataframes together in the following way:

  • the two dataframe might not have the same size nor the same amount of columns and rows, nor the same values
  • I want to obtain a dataframe that would be the multiplication of both dataframes when columns have the same naming and the index is the same in both dataframes
  • for example I want to multiply the value 33.24 from column 'value', row '2015-02-03 23:00:00' from df1 with the value from column 'value', row '2015-02-03 23:00:00' from df2. I want to do the same for all columns and rows in both dataframes

Any idea on how to do that?

The expected result would be a dataframe of the "inner" multiplication of both dataframe base on columns and index.

Many thanks for your help,

Peslier53
  • 587
  • 1
  • 7
  • 21

1 Answers1

0

For example you have following dataframes

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


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

The using mul followed by dropna

df1.mul(df2).dropna(thresh=1).dropna(1)# may not need the dropna(1)
Out[651]: 
     A
0  1.0
BENY
  • 317,841
  • 20
  • 164
  • 234