我有一个名为DataFrame
的maximoVerano
的DataFrame
并希望重新对其进行索引,因为我将用不同的年份索引附加其他数据帧。但是,当我想使用set-index
时,正如您在示例中看到的那样,我得到了错误代码:
KeyError: 'None of [2001] are in the columns'
>>> maximosVerano
demanda demanda31.5 ... fechaHoraMaxDem31.5 fechaHoraMaxDem63
0 28.037 4.1211 ... 2001-03-16 21:00:00 2001-01-15 22:00:00
[1 rows x 6 columns]
>>> type(maximosVerano)
<class 'pandas.core.frame.DataFrame'>
>>> maximosVerano.index
RangeIndex(start=0, stop=1, step=1)
>>> maximosVerano.set_index([anio], inplace=True)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python39\lib\site-packages\pandas\core\frame.py", line 4727, in set_index
raise KeyError(f"None of {missing} are in the columns")
KeyError: 'None of [2001] are in the columns'
>>> anio
2001
在这个简单的例子中,如何将索引设置为DataFrame
并获取索引2001?
请注意,索引的类型最初是一个范围。也许问题就在那里,但我不确定
试试
maximosVerano.index=[anio]
set_index()
是一个函数,用于将列设置为数据帧的新索引。要设置索引,可以使用简单赋值https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_index.html
set_index
期望keys
参数为label or array-like or list of labels/arrays
。您告诉它使用2002列作为索引(如anio
=2001
)尝试:
或
相关问题 更多 >
编程相关推荐