title: Pandas dataframe.loc method too slow
body: <p>I have a dataframe with +100K rows like this:</p>
  
  <pre><code>   user  document
  0  john      book
  1  jane   article
  2  jane      book
  3  jane      book
  4   jim   article
  5  john      book
  6   jim  blogpost
  7  jane  blogpost
  8  jane  blogpost
  9  jane  blogpost
  </code></pre>
  
  <p>I need the dataframe like this:</p>
  
  <pre><code>      blogpost  article  book
  john         1        3     0
  jane         0        0     1
  jim          4        0     2
  </code></pre>
  
  <p>That is, I need numbers of downloads for every <code>user, document</code> combination.    </p>
  
  <p>I'm doing <code>.groupby(['user', 'document'])</code> and then use <code>df.loc</code> to set download number:</p>
  
  <pre><code>df = pd.DataFrame(index=users, columns=documents)
  df.fillna(0, inplace=True)
  
  grouped = records.groupby(['user', 'document'])
  for elem in grouped:
      user, document = elem[0]
      downloads = len(elem[1])
      df.loc[user, document] = downloads
  </code></pre>
  
  <p>However, <code>df.loc</code> is very slow when used this way... I've commented out <code>df.loc..</code> line and found that the loop completes fast, so almost certainly it's <code>df.loc</code> access that is slow.</p>
  
  <p>How can I get this result faster?</p>
  
  <p>Minimum working example:</p>
  
  <pre><code>records = pd.DataFrame([
      ('john', 'book'), 
      ('jane', 'article'),
      ('jane','book'),
      ('jane','book'),
      ('jim', 'article'), 
      ('john', 'book'),
      ('jim', 'blogpost'), 
      ('jane', 'blogpost'),
      ('jane', 'blogpost'),
      ('jane', 'blogpost')
      ], columns=['user', 'document'])
  print(records)
  
  users = list(set(records['user']))
  users.sort()
  documents = list(set(records['document']))
  documents.sort()
  
  print(users)
  print(documents)
  
  df = pd.DataFrame(index=users, columns=documents)
  df.fillna(0, inplace=True)
  print(df)
  
  grouped = records.groupby(['user', 'document'])
  for elem in grouped:
      user, document = elem[0]
      downloads = len(elem[1])
      df.loc[user, document] = downloads
  </code></pre>