title: How do I use dictionary keys and values to rename columns in a pandas DataFrame?
body: <p>I am building functions to help me load data from the web. The problem I am trying to solve as far as loading data is that column names are different depending on the source. For example, Yahoo Finance data column headings look like this Open, High, Low, Close, Volume, Adj Close. Quandl.com will have data sets that have DATE,VALUE,date,value etc. The all upper case and lowercase throws everything off and Value and Adj. Close for the most part mean the same thing. I want to associate columns with different names but the same meaning to one value. For example Adj. Close and value both = AC; Open, OPEN, and open all = O.</p>
  
  <p>So I have a Csv file ("Functions//ColumnNameChanges.txt")  that stores dict() keys and values of column names.</p>
  
  <pre><code>Date,D
  Open,O
  High,H
  </code></pre>
  
  <p>and then I wrote this function to populate my dictionary </p>
  
  <pre><code>def DictKeyValuesFromText ():
  
      Dictionary = {}
      TextFileName = "Functions//ColumnNameChanges.txt"
      with open(TextFileName,'r') as f:
          for line in f:
              x = line.find(",")
              y = line.find("/")
              k = line[0:x]
              v = line[x+1:y]
  
              Dictionary[k] = v
      return Dictionary
  </code></pre>
  
  <p>This is the output of print(DictKeyValuesFromText())</p>
  
  <pre><code>{'': '', 'Date': 'D', 'High': 'H', 'Open': 'O'}
  </code></pre>
  
  <p>The next function is where my problems are at</p>
  
  <pre><code>def ChangeColumnNames(DataFrameFileLocation):
      x = DictKeyValuesFromText()
      df = pd.read_csv(DataFrameFileLocation)
      for y in df.columns:
          if y not in x.keys():
              i = input("The column " +  y +  " is not in the list, give a name:")
              df.rename(columns={y:i}) 
          else:
              df.rename(columns={y:x[y]})
  
      return df
  </code></pre>
  
  <p>df.rename is not working. This is the output I get print(ChangeColumnNames("Tvix_data.csv"))</p>
  
  <pre><code>The column Low is not in the list, give a name:L
  The column Close is not in the list, give a name:C
  The column Volume is not in the list, give a name:V
  The column Adj Close is not in the list, give a name:AC
              Date        Open        High         Low       Close    Volume  \
  0     2010-11-30  106.269997  112.349997  104.389997  112.349997         0
  1     2010-12-01   99.979997  100.689997   98.799998  100.689997         0
  2     2010-12-02   98.309998   98.309998   86.499998   86.589998         0
  </code></pre>
  
  <p>The columns names should be D, O, H, L, C, V. I am missing something any help would be appreciated. </p>