0

I'm trying to Make "ID" as Index, it throws error mentioned below and image:

enter image description here

obj= pd.read_csv("Supermarkets.csv")
obj

  ID   Address       City Country           Name  Number
0    1  Ecity-1  Bangalore   India   village mart       2
1    2  Ecity-2     Mysore   India           More       3
2    3  Ecity-3    Dharwad   India     Bigg bazar       1
3    4  Ecity-4     Haveri   India     Super Mart       2
4    5  Ecity-5     Badami   India  Kirani angadi       1

obj.set_index("ID")

Error:

Traceback (most recent call last):
  File "C:\Users\sharathkumar.chattar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexes\base.py", line 2656, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'ID'
3
  • Can you add the output of obj.columns.tolist() here? Commented Mar 13, 2019 at 18:46
  • what is print(obj.columns)? does this work after obj.columns = obj.columns.str.strip() ..?? Commented Mar 13, 2019 at 18:46
  • Yes, "ID" doesn't seem to be in obj.columns Commented Mar 13, 2019 at 18:57

1 Answer 1

2

I think the problem is you have trailing spaces in the 'ID' column name. I reproduced your data but trimmed off any excess spaces on import. You'll notice how the column names are all right justified. Your ID column appears not to be, likely because there are trailing spaces in the name. This also appears to be true for the other column names as well.

import pandas as pd

obj = pd.read_csv('Supermarkets.csv')
obj


   ID  Address       City Country           Name  Number
0   1  Ecity-1  Bangalore   India   village mart       2
1   2  Ecity-2     Mysore   India           More       3
2   3  Ecity-3    Dharwad   India     Bigg bazar       1
3   4  Ecity-4     Haveri   India     Super Mart       2
4   5  Ecity-5     Badami   India  Kirani angadi       1


obj.set_index("ID")


    Address       City Country           Name  Number
ID                                                   
1   Ecity-1  Bangalore   India   village mart       2
2   Ecity-2     Mysore   India           More       3
3   Ecity-3    Dharwad   India     Bigg bazar       1
4   Ecity-4     Haveri   India     Super Mart       2
5   Ecity-5     Badami   India  Kirani angadi       1

I can reproduce your same data table and avoid the error by using the full name with spaces:

obj = pd.read_csv('Supermarkets_spaces.csv')
obj


   ID   Address       City Country           Name   Number 
0     1  Ecity-1  Bangalore   India   village mart        2
1     2  Ecity-2     Mysore   India           More        3
2     3  Ecity-3    Dharwad   India     Bigg bazar        1
3     4  Ecity-4     Haveri   India     Super Mart        2
4     5  Ecity-5     Badami   India  Kirani angadi        1


obj.set_index("ID  ")


     Address       City Country           Name   Number 
ID                                                      
1     Ecity-1  Bangalore   India   village mart        2
2     Ecity-2     Mysore   India           More        3
3     Ecity-3    Dharwad   India     Bigg bazar        1
4     Ecity-4     Haveri   India     Super Mart        2
5     Ecity-5     Badami   India  Kirani angadi        1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.