pandas IndexError:访问pandas.DataFrame时索引越界

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33971055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:17:08  来源:igfitidea点击:

IndexError: indices are out-of-bounds when accessing pandas.DataFrame

pythonpandasdataframe

提问by Klausos Klausos

I want to access values of data by indices (xiand yishould be row indices), but I get the error IndexError: indices are out-of-bounds.

我想通过索引(xi并且yi应该是行索引)访问数据的值,但是我得到了错误IndexError: indices are out-of-bounds

for xi, yi in kd:
    X, y = data[xi], y_labs[yi]
  • data is pandas.core.frame.DataFrame
  • kd is numpy.ndarray
  • y_labs is numpy.ndarray
  • 数据是 pandas.core.frame.DataFrame
  • kd 是 numpy.ndarray
  • y_labs 是 numpy.ndarray

回答by priya khokher

You need to initialize your empty dataframe with index, and then access its locations. For instance

您需要使用索引初始化空数据框,然后访问其位置。例如

data = pd.DataFrame(index = range(len(yourFile)),columns = ['col1','col2'])

When you don't enter the index, pandascan't access cells or rows as there are not any.

当您不输入索引时,pandas无法访问单元格或行,因为没有任何单元格或行。