Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;索引器:位置索引器超出范围”;当他们';显然不是_Python_Pandas_Dataframe_Conditional Statements - Fatal编程技术网

Python &引用;索引器:位置索引器超出范围”;当他们';显然不是

Python &引用;索引器:位置索引器超出范围”;当他们';显然不是,python,pandas,dataframe,conditional-statements,Python,Pandas,Dataframe,Conditional Statements,下面是我正在使用的一些代码的MWE。我通过切片和一些条件慢慢地缩减初始数据帧,直到我只有所需的行。每一块五行实际上代表一个不同的对象,因此,当我缩减内容时,如果每一块五行中的任何一行符合条件,我希望保留它--这就是keep.index上的循环所完成的。不管怎样,当我完成后,我可以看到我想要的最终索引存在,但我得到一条错误消息,说“indexer-ror:positional indexer-out-bounds.”这里发生了什么 import pandas as pd import numpy

下面是我正在使用的一些代码的MWE。我通过切片和一些条件慢慢地缩减初始数据帧,直到我只有所需的行。每一块五行实际上代表一个不同的对象,因此,当我缩减内容时,如果每一块五行中的任何一行符合条件,我希望保留它--这就是keep.index上的循环所完成的。不管怎样,当我完成后,我可以看到我想要的最终索引存在,但我得到一条错误消息,说“indexer-ror:positional indexer-out-bounds.”这里发生了什么

import pandas as pd
import numpy as np

temp = np.random.rand(100,5)

df = pd.DataFrame(temp, columns=['First', 'Second', 'Third', 'Fourth', 'Fifth'])

df_cut = df.iloc[10:]

keep = df_cut.loc[(df_cut['First'] < 0.5) & (df_cut['Second'] <= 0.6)]

new_indices_to_use = []
for item in keep.index:
    remainder = (item % 5)
    add = np.arange(0-remainder,5-remainder,1)
    inds_to_use = item + add
    new_indices_to_use.append(inds_to_use)

new_indices_to_use = [ind for sublist in new_indices_to_use for ind in sublist]
final_indices_to_use = []
for item in new_indices_to_use:
    if item not in final_indices_to_use:
        final_indices_to_use.append(item)

final = df_cut.iloc[final_indices_to_use]
将熊猫作为pd导入
将numpy作为np导入
温度=np.随机兰特(100,5)
df=pd.DataFrame(临时,列=['First'、'Second'、'Third'、'Fourth'、'Fifth'])
df_cut=df.iloc[10:]

保持=df_-cut.loc[(df_-cut[‘第一’]<0.5)和(df_-cut[‘第二’],来自熊猫公司的文档(重点矿山):

Pandas提供了一套方法来获得纯粹基于整数的索引。语义紧跟python和numpy切片。这些是基于0的索引

你试图通过标签来使用它,这意味着你需要

从你的例子来看:

>>>print df_cut.iloc[89]
...
Name: 99, dtype: float64

>>>print df_cut.loc[89]
...
Name: 89, dtype: float64

您是否尝试过打印
最终索引以供使用
并验证它是否符合您的想法?在您的示例中,我得到
[10,11,…,98,99]
len(df\u cut)
给出了
90
。我不确定是否投票以打字错误结束,但在我看来,你应该使用
。loc
而不是
。iloc
在你的最后一行--
。iloc
用于位置访问,但你需要基于标签的访问。嗯,在我的MWE中,将其更改为。loc确实解决了问题。我必须这样做检查它在我的实际代码中是否有效——我可以发誓我已经试过了。为什么.iloc不在这里?这些是索引,.iloc的描述说它接受一个索引列表。另外,如果我使用相反的条件,然后执行final=df_cut.drop(df_cut.index[final_index_to_use]),我得到了同样的错误。因为
df_cut
的长度为90;高于
89
的索引将给出一个
索引器。多年后,来自其他人:谢谢。