title: Python Pandas set_index function: KeyError: "None of [] are in the columns"
body: <p>I am currently going through the book &quot;Hands-On machine learning... &quot; by Aurèlion Gèron. However, I am getting the following error message: (it is somewhat cumbersome to reproduce because the following two CSV downloads are required: <a href="https://stats.oecd.org/index.aspx?DataSetCode=BLI" rel="nofollow noreferrer">OECD</a> <a href="https://www.imf.org/external/pubs/ft/weo/2016/01/weodata/weorept.aspx?pr.x=32&amp;pr.y=8&amp;sy=2015&amp;ey=2015&amp;scsm=1&amp;ssd=1&amp;sort=country&amp;ds=.&amp;br=1&amp;c=512%2C668%2C914%2C672%2C612%2C946%2C614%2C137%2C311%2C962%2C213%2C674%2C911%2C676%2C193%2C548%2C122%2C556%2C912%2C678%2C313%2C181%2C419%2C867%2C513%2C682%2C316%2C684%2C913%2C273%2C124%2C868%2C339%2C921%2C638%2C948%2C514%2C943%2C218%2C686%2C963%2C688%2C616%2C518%2C223%2C728%2C516%2C558%2C918%2C138%2C748%2C196%2C618%2C278%2C624%2C692%2C522%2C694%2C622%2C142%2C156%2C449%2C626%2C564%2C628%2C565%2C228%2C283%2C924%2C853%2C233%2C288%2C632%2C293%2C636%2C566%2C634%2C964%2C238%2C182%2C662%2C453%2C960%2C968%2C423%2C922%2C935%2C714%2C128%2C862%2C611%2C135%2C321%2C716%2C243%2C456%2C248%2C722%2C469%2C942%2C253%2C718%2C642%2C724%2C643%2C576%2C939%2C936%2C644%2C961%2C819%2C813%2C172%2C199%2C132%2C733%2C646%2C184%2C648%2C524%2C915%2C361%2C134%2C362%2C652%2C364%2C174%2C732%2C328%2C366%2C258%2C734%2C656%2C144%2C654%2C146%2C336%2C463%2C263%2C528%2C268%2C923%2C532%2C738%2C944%2C578%2C176%2C537%2C534%2C742%2C536%2C866%2C429%2C369%2C433%2C744%2C178%2C186%2C436%2C925%2C136%2C869%2C343%2C746%2C158%2C926%2C439%2C466%2C916%2C112%2C664%2C111%2C826%2C298%2C542%2C927%2C967%2C846%2C443%2C299%2C917%2C582%2C544%2C474%2C941%2C754%2C446%2C698%2C666&amp;s=NGDPDPC&amp;grp=0&amp;a=" rel="nofollow noreferrer">IMF</a>.</p>
  <p><strong>Error message</strong>:</p>
  <blockquote>
  <p>File &quot;C:\Users\xxx\Miniconda3\lib\site-packages\pandas\core\frame.py&quot;,
  line 4548, in set_index
  raise KeyError(f&quot;None of {missing} are in the columns&quot;)</p>
  <p>KeyError: &quot;None of ['Country'] are in the columns&quot;</p>
  </blockquote>
  <p>The code:</p>
  <pre><code>import matplotlib.pyplot as plt 
  import numpy as np 
  import pandas as pd 
  import sklearn.linear_model
  
  oecd_bli = pd.read_csv(&quot;BLI_24092020220751169.csv&quot;, thousands =',')
  
  gdp_per_capita = pd.read_csv(&quot;gdp_per_capita.csv&quot;, thousands =',', delimiter ='\t', encoding =' latin1', na_values=&quot;n/a&quot;)
  
  def prepare_country_stats(oecd_bli, gdp_per_capita):
      oecd_bli = oecd_bli[oecd_bli[&quot;INEQUALITY&quot;]==&quot;TOT&quot;]
      oecd_bli = oecd_bli.pivot(index=&quot;Country&quot;, columns=&quot;Indicator&quot;, values=&quot;Value&quot;)
      gdp_per_capita.rename(columns={&quot;2015&quot;:&quot;GDP per capita&quot;}, inplace=True)
      gdp_per_capita.set_index(&quot;Country&quot;, inplace=True)
      full_country_stats = pd.merge(left=oecd_bli, right=gdp_per_capita,
                                    left_index=True, right_index=True)
      full_country_stats.sort_values(by=&quot;GDP per capita&quot;, inplace=True)
      remove_indices = [0, 1, 6, 8, 33, 34, 35]
      keep_indices = list(set(range(36)) - set(remove_indices))
      return full_country_stats[[&quot;GDP per capita&quot;, 'Life satisfaction']].iloc[keep_indices]
  
  country_stats = prepare_country_stats(oecd_bli, gdp_per_capita) 
  X = np.c_[country_stats[&quot;GDP per capita&quot;]] 
  y = np.c_[country_stats[&quot;Life satisfaction&quot;]] 
  
  # Visualize the data 
  country_stats.plot( kind ='scatter', X =&quot;GDP per capita&quot;, y ='Life satisfaction') 
  plt.show() 
  
  # Select a linear model 
  model = sklearn.linear_model.LinearRegression() 
  
  # Train the model 
  model.fit(X, y) 
  
  # Make a prediction for Cyprus 
  X_new = [[22587]] 
  # Cyprus's GDP per capita 
  
  print( model.predict(X_new))
  </code></pre>
  <p>However, already in the function I get stuck. The error seems to be related to the <code>set_index</code> command, which I thought was a very reliable function. Of course, in my CSV file the <code>Country</code> column is present.</p>
  <p>Here is a screenshot of the <code>gdp_per_capita</code> CSV.</p>
  <p><img src="https://i.stack.imgur.com/wbHzg.png" alt="gdp_per_capita" /></p>
  <p>If anyone takes the time to reproduce, it would be highly appreciated.</p>