Home > OS >  Pandas multiply two dataframes together without looping
Pandas multiply two dataframes together without looping

Time:10-01

I have two df i want multiplied together without looping through.

For each of the below rows, i want to iterate the below and for each of the arm angle

    sample  pos arm1_angle  arm2_angle  arm3_angle  arm4_angle
0   0   0.000000    0.000000    0.250000    0.500000    0.750000
1   1   0.134438    0.134438    0.384438    0.634438    0.884438
2   2   0.838681    0.838681    0.088681    0.338681    0.588681
3   3   1.755019    0.755019    0.005019    0.255019    0.505019
4   4   3.007274    0.007274    0.257274    0.507274    0.757274
5   5   4.186825    0.186825    0.436825    0.686825    0.936825
6   6   3.455513    0.455513    0.705513    0.955513    0.205513
7   7   4.916564    0.916564    0.166564    0.416564    0.666564
8   8   2.876257    0.876257    0.126257    0.376257    0.626257
9   9   2.549585    0.549585    0.799585    0.049585    0.299585
10  10  1.034488    0.034488    0.284488    0.534488    0.784488

multiply by the below table and concatenate. For example, if there are 10k rows above, the sample size will be 10k x 27 = 270,000 rows. so for index 0, multiply the entire below table with 0 for arm1, 0.25 for arm2, 0.5 for arm3, and 0.75 for arm3.

I can easily loop through, multiple and concatenate. Is there a more efficient way?

    id  radius  bag_count   arm1    arm2    arm3    arm4
0   1   0.440   4   1.0 0.0 0.0 0.0
1   2   0.562   8   0.0 1.0 0.0 0.0
2   3   0.666   12  0.0 0.0 1.0 0.0
3   4   0.818   16  1.0 0.0 0.0 0.0
4   5   0.912   16  0.0 1.0 0.0 0.0
5   6   1.022   20  0.0 0.0 1.0 0.0
6   7   1.120   24  1.0 0.0 0.0 0.0
7   8   1.220   28  0.0 1.0 0.0 0.0
8   9   1.350   32  0.0 0.0 1.0 0.0
9   10  1.460   36  1.0 0.0 1.0 0.0
10  11  1.570   40  0.0 1.0 0.0 1.0
11  12  1.680   44  1.0 0.0 1.0 0.0
12  13  1.800   44  0.0 1.0 0.0 1.0
13  14  1.920   48  1.0 0.0 1.0 0.0
14  15  2.030   52  0.0 1.0 0.0 1.0
15  16  2.140   56  1.0 0.0 1.0 0.0
16  17  2.250   60  0.0 1.0 1.0 1.0
17  18  2.360   64  1.0 0.0 1.0 1.0
18  19  2.470   68  1.0 1.0 0.0 1.0
19  20  2.580   72  1.0 1.0 1.0 0.0
20  21  2.700   72  0.0 1.0 1.0 1.0
21  22  2.810   76  1.0 0.0 1.0 1.0
22  23  2.940   80  1.0 1.0 0.0 1.0
23  24  3.060   84  1.0 1.0 1.0 0.0
24  25  3.180   88  1.0 1.0 1.0 1.0
25  26  3.300   92  1.0 1.0 1.0 1.0
26  27  3.420   96  1.0 1.0 1.0 1.0

CodePudding user response:

Use cross join for all rows and then select rows with arm and multiple:

df22 = df2.filter(like='arm')
cols = df1.filter(like='arm').columns

df = df1.merge(df22, how='cross')
df[cols] = df[cols].mul(df[df22.columns].to_numpy())
df = df.drop(df22.columns, axis=1)
  • Related