Suresh Raja
Suresh Raja

Reputation: 745

Divide every other column with values in the last column

I have data as shown below. I would like to divide every other row with the weight column. Is there an automated way to do this? My data has about several columns.

What I looking for is the result of A/Weight should be replaced in column A and the result of C/Weight should be replaced in Column C and so on until the last pair of columns.

          A            B           C           D       Weight
 0   2.056494    -3.002088   0.516822    -1.338846      0.40
 1   0.082295    1.387734    -0.495226   1.119553       0.50 
 2   0.298618    -0.130158   0.804705    -0.120110      0.25
 3   0.178088    1.137238    1.331856    -0.472720      0.50
 4   -0.378670   1.649041    -0.240723   2.044113       0.65
 5   3.602587    1.152502    -0.170646   -0.961922      0.50
 6   -0.285846   -0.154891   1.492879    0.752487       0.56
 7   -0.412809   1.076796    -2.001025   -0.954021      0.25

I have something like this but it does not work:

results=results.iloc[:, 0::2].div(results['Weight'], axis=0)

The code below works for every other column (B/A and D/C). But I need the denominator static with just the 'Weight' column.

results_201.iloc[:,1::2] /= results_201.iloc[:,::2].values

Thanks for reading this and for your help!

Upvotes: 2

Views: 65

Answers (1)

Divakar
Divakar

Reputation: 221534

We would need broadcasting here. So, one approach would involve using the underlying array data for in-situ edit, like so -

df.iloc[:,:-1:2] = df.iloc[:,:-1:2].values / df.iloc[:,[-1]].values

Sample run -

In [62]: df
Out[62]: 
        A       B       C       D  Weight
0  2.0565 -3.0021  0.5168 -1.3388  0.4000
1  0.0823  1.3877 -0.4952  1.1196  0.5000
2  0.2986 -0.1302  0.8047 -0.1201  0.2500
3  0.1781  1.1372  1.3319 -0.4727  0.5000
4 -0.3787  1.6490 -0.2407  2.0441  0.6500
5  3.6026  1.1525 -0.1706 -0.9619  0.5000
6 -0.2858 -0.1549  1.4929  0.7525  0.5600
7 -0.4128  1.0768 -2.0010 -0.9540  0.2500

In [63]: df.iloc[:,:-1:2] = df.iloc[:,:-1:2].values / df.iloc[:,[-1]].values

In [64]: df
Out[64]: 
        A       B       C       D  Weight
0  5.1412 -3.0021  1.2921 -1.3388  0.4000
1  0.1646  1.3877 -0.9905  1.1196  0.5000
2  1.1945 -0.1302  3.2188 -0.1201  0.2500
3  0.3562  1.1372  2.6637 -0.4727  0.5000
4 -0.5826  1.6490 -0.3703  2.0441  0.6500
5  7.2052  1.1525 -0.3413 -0.9619  0.5000
6 -0.5104 -0.1549  2.6659  0.7525  0.5600
7 -1.6512  1.0768 -8.0041 -0.9540  0.2500

An easier on eyes alternative would be to get the entire data as array view and process on that array itself -

In [96]: df
Out[96]: 
        A       B       C       D  Weight
0  2.0565 -3.0021  0.5168 -1.3388  0.4000
1  0.0823  1.3877 -0.4952  1.1196  0.5000
2  0.2986 -0.1302  0.8047 -0.1201  0.2500
3  0.1781  1.1372  1.3319 -0.4727  0.5000
4 -0.3787  1.6490 -0.2407  2.0441  0.6500
5  3.6026  1.1525 -0.1706 -0.9619  0.5000
6 -0.2858 -0.1549  1.4929  0.7525  0.5600
7 -0.4128  1.0768 -2.0010 -0.9540  0.2500

In [97]: a = df.values

In [98]: a[:,:-1:2] /= a[:,[-1]]

In [99]: df
Out[99]: 
        A       B       C       D  Weight
0  5.1412 -3.0021  1.2921 -1.3388  0.4000
1  0.1646  1.3877 -0.9905  1.1196  0.5000
2  1.1945 -0.1302  3.2188 -0.1201  0.2500
3  0.3562  1.1372  2.6637 -0.4727  0.5000
4 -0.5826  1.6490 -0.3703  2.0441  0.6500
5  7.2052  1.1525 -0.3413 -0.9619  0.5000
6 -0.5104 -0.1549  2.6659  0.7525  0.5600
7 -1.6512  1.0768 -8.0041 -0.9540  0.2500

Upvotes: 1

Related Questions