title: Python: weighted median algorithm with pandas
body: <p>I have a dataframe that looks like this: </p>
  
  <pre><code>Out[14]:
      impwealth  indweight
  16     180000     34.200
  21     384000     37.800
  26     342000     39.715
  30    1154000     44.375
  31     421300     44.375
  32    1210000     45.295
  33    1062500     45.295
  34    1878000     46.653
  35     876000     46.653
  36     925000     53.476
  </code></pre>
  
  <p>I want to calculate the weighted median of the column <code>impwealth</code> using the frequency weights in <code>indweight</code>. My pseudo code looks like this: </p>
  
  <pre><code># Sort `impwealth` in ascending order 
  df.sort('impwealth', 'inplace'=True)
  
  # Find the 50th percentile weight, P
  P = df['indweight'].sum() * (.5)
  
  # Search for the first occurrence of `impweight` that is greater than P 
  i = df.loc[df['indweight'] &gt; P, 'indweight'].last_valid_index()
  
  # The value of `impwealth` associated with this index will be the weighted median
  w_median = df.ix[i, 'impwealth']
  </code></pre>
  
  <p>This method seems clunky, and I'm not sure it's correct. I didn't find a built in way to do this in pandas reference. What is the best way to go about finding weighted median?</p>