无法在python中使用pandas包使用set_index()

import pandas 

df6 = pandas.read_csv("https://www.pythonhow.com/supermarkets.csv")

df6.set_index('test')

我希望输出添加一个行标题“测试”,但在我的 jupyter 笔记本中返回以下错误。


KeyError                                  Traceback (most recent call last)

c:\users\sunair raj\appdata\local\programs\python\python37-32\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)

   3077             try:

-> 3078                 return self._engine.get_loc(key)

   3079             except KeyError:


pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()


pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()


pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()


pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()


KeyError: 'fdfdf'

在处理上述异常的过程中,又发生了一个异常:


小唯快跑啊
浏览 457回答 2
2回答

慕村9548890

.set_index()正在尝试获取一列并将其设为索引。由于您没有任何名为 的列test,因此它失败了。不确定您是如何收到您发布的错误消息的,因为fdfdf找不到任何地方。你到底想做什么?输出应该是什么样的?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python