Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas multiply each dataset row by multiple vectors

df = {1,2,3
      4,5,6
      7,8,9,
      10,11,12
}

weights={[1,3,3],[2,2,2],[3,1,1]}

I want to multiply my df with every line of matrix weights(so I'll have like three different df, one for each vector of weights, and to combine each df by keeping the biggest line of values). Ex:

df0=df * weights[0]={1,6,9
                    4,15,18,
                    7,24,27
                    10,33,36
                    } 

df1=df*wieghts[1]={2,4,6,
                   8,19,12,
                   14,16,18,
                   20,22,24
                  }

df2=df*wieghts[2]={3,2,3,
                   12,5,6,
                   21,8,9,
                   30,11,12
                  }

and

final_df_lines=max{df0,df1,df2}={1,6,9 - max line line from df0, 
                                     4,15,18, - max line from df0, 
                                     7,24,27 - max line from df0, 
                                     10,33,36 - max line from df0, 
                                     } 

In this example all max were from df0 ... but they could be from any of the three df. Max line is just adding the numbers from the same line..

I need to do this things vectorized(without any loops or if...) how do I do this? is it possible at least? I really need welp :( for 2 days I'm searching the internet to do this... I did not work in python for too long...

like image 368
Andrei Bratu Avatar asked Nov 29 '25 10:11

Andrei Bratu


1 Answers

you can try of concatenating all weights mulitpied columns as one dataframe with suffix of column represeting each weight , and by grouping with respect to the weight it multiplied get max summation of index

with max index weight you can multiply the dataframe

df2 = pd.concat([(df*i).add_suffix('__'+str(i)) for i in weights],axis=1).T
                0   1   2   3
0__[1, 3, 3]    1   4   7   10
1__[1, 3, 3]    6   15  24  33
2__[1, 3, 3]    9   18  27  36
0__[2, 2, 2]    2   8   14  20
1__[2, 2, 2]    4   10  16  22
2__[2, 2, 2]    6   12  18  24
0__[3, 1, 1]    3   12  21  30
1__[3, 1, 1]    2   5   8   11
2__[3, 1, 1]    3   6   9   12

#   by grouping with respect to the weight it multiplied, get max index
a = df2.groupby(df2.index.str.split('__').str[1]).apply(lambda x: x.sum()).idxmax()

#  max weights with respect to summation of rows
df['idxmax'] = a.str.slice(1,-1).str.split(',').apply(lambda x: list(map(int,x)))

c    [1, 3, 3]
d    [1, 3, 3]
3    [1, 3, 3]
4    [1, 3, 3]
dtype: object

df.apply(lambda x: x.loc[df.columns.difference(['idxmax'])] * x['idxmax'],1)

   0    1   2
0   1   6   9
1   4   15  18
2   7   24  27
3   10  33  36
like image 130
Naga kiran Avatar answered Dec 01 '25 22:12

Naga kiran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!