啊啊啊啊啊吖

2019-03-14   阅读量: 1912

数据分析师 Python编程

如何通过python中的pandas更改CSV文件中的索引?

我正在尝试将“ID”作为索引,它会抛出下面提到的错误并且图像:

在此输入图像描述

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")

错误

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'
0.0000 0 6 关注作者 收藏

评论(1)

啊啊啊啊啊吖
2019-03-14

在'ID'列名称中有尾随空格。我复制了您的数据,但在导入时删除了多余的空间。你会注意到列名是如何正确对齐的。您的ID列似乎不是,可能是因为名称中有尾随空格。对于其他列名称,这似乎也是如此。

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

0.0000 0 0 回复

推荐课程