title: pandas data frame - reduce with initial value
body: <p>I'm moving some of my <code>R</code> stuff to <code>Python</code>, hence I have to use <code>pandas.DataFrame</code>s. There are several things I'd like to optimise.</p>
  
  <p>Suppose we've got a table</p>
  
  <pre><code>key value
  abc 1
  abc 2
  abd 1
  </code></pre>
  
  <p>and we want to get a dictionary of form <code>{key -&gt; list[values]}</code>. Here is how I get this done right now. </p>
  
  <pre><code>from pandas import DataFrame
  from StringIO import StringIO
  
  
  def get_dict(df):
      """
      :param df:
      :type df: DataFrame
      """
      def f(accum, row):
          """
          :param accum:
          :type accum: dict
          """
          key, value = row[1]
          return accum.setdefault(key, []).append(value) or accum
      return reduce(f, df.iterrows(), {})
  
  
  table = StringIO("key\tvalue\nabc\t1\nabc\t2\nabd\t1")
  parsed_table = [row.rstrip().split("\t") for row in table]
  df = DataFrame(parsed_table[1:], columns=parsed_table[0])
  result = get_dict(df)  # -&gt; {'abc': ['1', '2'], 'abd': ['1']}
  </code></pre>
  
  <p>Two things I don't like about it:</p>
  
  <ol>
  <li>The fact that built-in <code>reduce</code> uses standard Python iteration protocol that kills the speed of NumPy-based data structures like <code>DataFrame</code>. I know that <code>DataFrame.apply</code> has a <code>reduce</code> mode, but it doesn't take a starting value like <code>dict</code>. </li>
  <li><em>(a minor drawback)</em> The fact that I have to use indexing to get specific values from rows. I wish I could access specific fields in a row by name like in <code>R</code>, i.e. <code>row$key</code> instead of <code>row[1][0]</code></li>
  </ol>
  
  <p>Thank you in advance</p>