当iterrow()时索引越界怎么可能?

2024-04-26 07:27:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到错误消息:

5205
(5219, 25)
5221
(5219, 25)
Traceback (most recent call last):
  File "/Users/Chu/Documents/dssg2018/sa4.py", line 44, in <module>
    df.loc[idx,word]=len(df.iloc[indices[idx]][df[word]==1])/\
IndexError: index 5221 is out of bounds for axis 0 with size 5219

当我遍历数据帧时,索引来自迭代器。我不知道这怎么可能?idx直接来自数据帧

^{pr2}$

iloc更改为loc可以得到

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/Chu/Documents/dssg2018/sa4.py
(-124.60334244261675, 49.36453144316216, -121.67106179949566, 50.863501888419826)
27
(5219, 25)
/Users/Chu/Documents/dssg2018/sa4.py:42: FutureWarning: 
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike
  df.loc[idx,word]=len(df.loc[indices[idx]][df[word]==1])/\
/Users/Chu/Documents/dssg2018/sa4.py:42: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  df.loc[idx,word]=len(df.loc[indices[idx]][df[word]==1])/\
Traceback (most recent call last):
  File "/Users/Chu/Documents/dssg2018/sa4.py", line 42, in <module>
    df.loc[idx,word]=len(df.loc[indices[idx]][df[word]==1])/\
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/frame.py", line 2133, in __getitem__
    return self._getitem_array(key)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/frame.py", line 2173, in _getitem_array
    key = check_bool_indexer(self.index, key)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/indexing.py", line 2023, in check_bool_indexer
    raise IndexingError('Unalignable boolean Series provided as '
pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match

Tags: inpypandasdflenlineusersloc
1条回答
网友
1楼 · 发布于 2024-04-26 07:27:48

您的index不是从0到{},这将使df.iloc[idx]越界

例如

df = pd.DataFrame({'a': [0, 1]},index=[1,100])

for idx,row in df.iterrows():
    print(idx)
    print(row)

1
a    0
Name: 1, dtype: int64
100
a    1
Name: 100, dtype: int64

当你这么做的时候

^{pr2}$

IndexError: single positional indexer is out-of-bounds

但是当你做.loc时,你得到了预期的输出。在

df.loc[100]
Out[23]: 
a    1
Name: 100, dtype: int64

从文件中:

.iloc:iloc[]主要基于整数位置

.loc:.loc[]主要基于标签

解决方案:

使用.loc或{}

相关问题 更多 >