title: How to efficiently update the column names in a pandas dataframe?
body: <p>I would like to refactor the following code:</p>
  
  <pre><code>labels = list(df.columns)
  labels[0] = labels[0].replace(' ', '_')
  labels[1] = labels[1].replace(' ', '_')
  labels[2] = labels[2].replace(' ', '_')
  labels[3] = labels[3].replace(' ', '_')
  labels[5] = labels[5].replace(' ', '_')
  labels[6] = labels[6].replace(' ', '_')
  df.columns = labels
  
  df.head()
  </code></pre>
  
  <p>However I keep getting a ValueError when I run the code in Jupyter. I want to create integer indexes. This is what I refactored the code into:</p>
  
  <pre><code>labels = list(df.columns)
  for label in labels: 
      label = int(label)
      labels[label] = labels[label].replace(' ', '_')
  df.columns = labels
  
  df.head()
  </code></pre>
  
  <p>How do I fix this? Here is an example of what I am trying to achieve with the dataframe(df) being renamed to remove spaces:</p>
  
  <pre><code>new_df = df.rename(columns={'fixed acidity': 'fixed_acidity',
                               'volatile acidity': 'volatile_acidity',
                               'citric acid': 'citric_acid',
                               'residual sugar': 'residual_sugar',
                               'free sulfur dioxide': 'free_sulfur_dioxide',
                               'total sulfur dioxide': 'total_sulfur_dioxide'
                              })
  new_df.head()
  </code></pre>