title: Pandas Group Weighted Average of Multiple Columns
body: <p>Say I have the following dataframe:</p>
  
  <pre><code>&gt;&gt;&gt; df=pd.DataFrame({'category':['a','a','b','b'],
  ... 'var1':np.random.randint(0,100,4),
  ... 'var2':np.random.randint(0,100,4),
  ... 'weights':np.random.randint(0,10,4)})
  &gt;&gt;&gt; df
    category  var1  var2  weights
  0        a    37    36        7
  1        a    47    20        1
  2        b    33     7        6
  3        b    16     6        8
  </code></pre>
  
  <p>I can calculate the weighted average of a 'var1' as such:</p>
  
  <pre><code>&gt;&gt;&gt; Grouped=df.groupby('category')
  &gt;&gt;&gt; GetWeightAvg=lambda g: np.average(g['var1'], weights=g['weights'])
  &gt;&gt;&gt; Grouped.apply(GetWeightAvg)
  category
  a    38.250000
  b    23.285714
  dtype: float64
  </code></pre>
  
  <p>However I am wondering if there is a way I can write my function and apply it to my grouped object such that I can specify when applying it, which column I want to calculate for (or both). Rather than have 'var1' written into my function, I'd like to be able to specify when applying the function.</p>
  
  <p>Just as I can get an unweighted average of both columns like this:</p>
  
  <pre><code>&gt;&gt;&gt; Grouped[['var1','var2']].mean()
            var1  var2
  category            
  a         42.0  28.0
  b         24.5   6.5
  </code></pre>
  
  <p>I'm wondering if there is a parallel way to do that with weighted averages.</p>