title: Python - Calculating in dataframes
body: <p>I am having problems with calculating in dataframes.</p>
  
  <p>I am calculating the </p>
  
  <ul>
  <li>%-change per column</li>
  <li>%-share per column based on the sum of the row</li>
  </ul>
  
  <p>Now I want to calculate the %-change per column based on the %-share per column. I would do that like the following:</p>
  
  <p>(col A %-change * col A %-share) + (col B %-change * col B %-share) + (col C %-change * col C %-share) and so on.</p>
  
  <p>I thought of two ways to do that, though neither is working… And – of course – I would like to do it as dynamically as possible as the number of columns will vary. </p>
  
  <p>Here is some code to get a fitting dataframe:</p>
  
  <pre><code>    import pandas as pd
      import numpy as np
  
      df1 = pd.DataFrame(np.array([
      ['2019-04-29', 5, 5, 10],
      ['2019-04-28', 10, 10, 50],
      ['2019-04-27', 20, 5, 100]]),
      columns=['Date', 'attr1', 'attr2', 'attr3'])
  
      df1["attr1"] = pd.to_numeric(df1["attr1"])
      df1["attr2"] = pd.to_numeric(df1["attr2"])
      df1["attr3"] = pd.to_numeric(df1["attr3"])
  
      df1 = df1.set_index(['Date'])
      col_list= list(df1)
  
      df1['sum'] = df1[col_list].sum(axis=1)
  
      for i in col_list:
      df1[i + ' %-change'] = df1[i].pct_change()*100
      df1[i + " %-share"] = df1[i]/ df1['sum']
  </code></pre>
  
  <p>Idea 1: 
  Basically I am trying the formula mentioned above. So multiply the %-share and the %-change of each column and sum these up. I thought about using .sum but don´t know how to use it in this specific situation.</p>
  
  <pre><code>df1[i + ' %-change incl share'] = (df1[i + ' %-share'] * df1[i + ' %-change'])
  </code></pre>
  
  <p>Idea 2:
  Thought about doing this in two steps. Step one would be to multiply the %-share and the %-change of each column. The second step would be to sum up the new columns in a new code line. The problem is I can´t make it dynamically. I would have to manually select the columns I need to summarize, but if I am having let’s say a 100 columns that would be tedious work indeed.</p>
  
  <p>I thought about writing the newly calculated columns into a new dataframe so I´ve got all the columns I need, but the data in the dataframe make no sense to me.</p>
  
  <pre><code>df2[i + ' %-change incl share'] = (df1[i + ' %-share'] * df1[i + ' %-change'])
  </code></pre>
  
  <p>As always any help is greatly appreciated.</p>
  
  <p>Thanks a lot! </p>