title: Replace values in a pandas series via dictionary efficiently
body: <p>How to replace values in a Pandas series <code>s</code> via a dictionary <code>d</code> has been asked and re-asked many times.</p>
  
  <p>The recommended method (<a href="https://stackoverflow.com/a/40855794/9209546">1</a>, <a href="https://stackoverflow.com/a/40528697/9209546">2</a>, <a href="https://stackoverflow.com/a/45355744/9209546">3</a>, <a href="https://stackoverflow.com/a/20250996/9209546">4</a>) is to either use <code>s.replace(d)</code> or, occasionally, use <code>s.map(d)</code> if all your series values are found in the dictionary keys.</p>
  
  <p>However, performance using <code>s.replace</code> is often unreasonably slow, often 5-10x slower than a simple list comprehension.</p>
  
  <p>The alternative, <code>s.map(d)</code> has good performance, but is only recommended when all keys are found in the dictionary.</p>
  
  <p>Why is <code>s.replace</code> so slow and how can performance be improved?</p>
  
  <pre><code>import pandas as pd, numpy as np
  
  df = pd.DataFrame({'A': np.random.randint(0, 1000, 1000000)})
  lst = df['A'].values.tolist()
  
  ##### TEST 1 #####
  
  d = {i: i+1 for i in range(1000)}
  
  %timeit df['A'].replace(d)                          # 1.98s
  %timeit [d[i] for i in lst]                         # 134ms
  
  ##### TEST 2 #####
  
  d = {i: i+1 for i in range(10)}
  
  %timeit df['A'].replace(d)                          # 20.1ms
  %timeit [d.get(i, i) for i in lst]                  # 243ms
  </code></pre>
  
  <p><strong>Note:</strong> This question is not marked as a duplicate because it is looking for specific advice on <em>when to use</em> different methods given different datasets. This is explicit in the answer and is an aspect not usually addressed in other questions.</p>