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

Python 当iterrow()时索引越界这怎么可能?,python,pandas,dataframe,Python,Pandas,Dataframe,我收到错误消息: 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 boun

我收到错误消息:

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
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
/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)
/用户/Chu/Documents/dssg2018/sa4.py:42:未来警告:
通过列表喜欢。丢失标签的loc或[]将引发
KeyError以后,您可以使用.reindex()作为替代方法。
请参阅此处的文档:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc reindex列表式
df.loc[idx,word]=len(df.loc[index[idx]][df[word]=1])/\
/Users/Chu/Documents/dssg2018/sa4.py:42:UserWarning:Boolean系列键将被重新索引以匹配数据帧索引。
df.loc[idx,word]=len(df.loc[index[idx]][df[word]=1])/\
回溯(最近一次呼叫最后一次):
文件“/Users/Chu/Documents/dssg2018/sa4.py”,第42行,在
df.loc[idx,word]=len(df.loc[index[idx]][df[word]=1])/\
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/pandas/core/frame.py”,第2133行,在__
返回self.\u getitem\u数组(键)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/pandas/core/frame.py”,第2173行,在_getitem_数组中
key=check\u bool\u索引器(self.index,key)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/pandas/core/index.py”,第2023行,在check\u bool\u indexer中
raise IndexingError('作为提供的不可对齐布尔序列'
pandas.core.indexing.IndexingError:作为索引器提供的不可对齐的布尔序列(布尔序列的索引和索引对象的索引不匹配)

您的
索引
不是从
0
len(df)-1
,这将使
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
那么当你这么做的时候

df.iloc[100]
索引器:单个位置索引器超出范围

但是当您执行
.loc
时,您会得到预期的输出

df.loc[100]
Out[23]: 
a    1
Name: 100, dtype: int64
从文件中:

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

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

解决方案:


使用
.loc
df=df.reset_index(drop=True)

在这种情况下,我应该如何解决此问题?@单调使用loc@monotonic顺便说一句,如果我的答案有帮助,不要忘记投票和投票accept@monotonic你指的是iloc还是loc?
df.loc[100]
Out[23]: 
a    1
Name: 100, dtype: int64