-1

I very new in python and I'm trying to have a first approach in pandas.

I have two dataframes, two tabs, made in this way:

df1

index | value_1 | value_2 | value_3 | value_4
0        1          2.5        3        5.5
1        5           1         2         3
2        8.58        4         1.5       1
3        2           0         0         0 
4        7           17        8         9`

df2

index | coeff_1 | coeff_2 | coeff_3 
0        1           0         1       
1        0           1         0         
2        0           1         1      
3        1           0         0          
4        1           1         0 

considering the first row of df1

 | value_1 | value_2 | value_3 | value_4
      1          2.5        3        5.5`

and considering the first column of df2

| coeff_1 |
     1             
     0               
     1                  
     1

I would like to do 1*1 + 2.5*0+1*3+5.5*1

I tried to create the dataframes but I was not able to do this operation.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
madpynoob
  • 1
  • 1
  • Welcome to [Stack Overflow](https://stackoverflow.com/tour). Please check [How to Ask](https://stackoverflow.com/help/how-to-ask). It's also best to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to enable others to help you. And check [How to make pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – MagnusO_O Nov 01 '22 at 21:48

1 Answers1

0

Here is one way you can do it:

import pandas as pd
  
# initialize list of lists
data_row = [[1, 2], [3, 4]]
data_col = [[5, 6], [7, 8]]
# Create the pandas DataFrame
df_row = pd.DataFrame(data_row)
df_col = pd.DataFrame(data_col)
#Transpose the df_col
df_col = df_col.T
df_mul = df_row.multiply(df_col)
df_mul.sum(axis=1)

Hope this helps!

anonymous
  • 1
  • 1
  • Dear @anonymus thank you for your answer I thought about the transpose but the interesting things are df_row.multiply and df_mul.sum. I'll try – madpynoob Nov 01 '22 at 14:06