Search code examples
pythonnumpypandasr-factor

Pandas: process cell dependencies


I'm new to Pandas so please bear with me; I have a dataframe A:

   one  two three
0    1    5     9
1    2    6    10
2    3    7    11
3    4    8    12

And a dataframe B, which represents relationships between columns in A:

      one two  # these get mutated in place
three   1   1
one     0   0

I need to use this to multiply values in-place with values in other columns. The output should be:

   one  two three
0    9   45     9
1    20  60    10
2    33  77    11
3    48  96    12

So in this case I have made the adjustments for each row:

one *= three
two *= three

Is there an efficient way to use this with Pandas / Numpy?


Solution

  • Take a look at here

    In [37]: df
    Out[37]: 
       one  two  three
    0    1    5      9   
    1    2    6     10  
    2    3    7     11  
    3    4    8     12  
    
    In [38]: df['one'] *= df['three']
    
    In [39]: df['two'] *= df['three']
    
    In [40]: df
    Out[40]: 
       one  two  three
    0    9   45      9   
    1   20   60     10  
    2   33   77     11  
    3   48   96     12