Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 是否可以使用iloc方法查找大数据帧(80000行,6列)中某些行的索引位置?_Python_Python 3.x_Pandas_Indexing - Fatal编程技术网

Python 是否可以使用iloc方法查找大数据帧(80000行,6列)中某些行的索引位置?

Python 是否可以使用iloc方法查找大数据帧(80000行,6列)中某些行的索引位置?,python,python-3.x,pandas,indexing,Python,Python 3.x,Pandas,Indexing,我的数据框有六列浮点数据和大约80000行。其中一列为当前列,有时为负值。我想在当前值为负值时查找索引位置。我的代码如下: currnet_index = my_df[(my_df["Current"]<0)].index.tolist() print(current_index[:5]) 这很好。是否可以使用iloc方法编写此代码?我尝试了以下代码,但它给出了错误。我想知道哪种方法是最好最快的 current_index = my_df.iloc[(my_df["Current"]&l

我的数据框有六列浮点数据和大约80000行。其中一列为当前列,有时为负值。我想在当前值为负值时查找索引位置。我的代码如下:

currnet_index = my_df[(my_df["Current"]<0)].index.tolist()
print(current_index[:5])
这很好。是否可以使用iloc方法编写此代码?我尝试了以下代码,但它给出了错误。我想知道哪种方法是最好最快的

current_index = my_df.iloc[(my_df["Current"]<0)]

您可以简单地使用以下命令

my_df.ix[my_df['Current']<0].index.values
使用iloc时,需要使用布尔数组,而不是布尔序列。为此,您可以使用。下面是一个演示:

df = pd.DataFrame({'Current': [1, 3, -4, 9, -3, 1, -2]})

res = df.iloc[df['Current'].lt(0).values].index

# Int64Index([2, 4, 6], dtype='int64')

顺便说一句,loc可以处理数组或序列。

iloc根据给定的位置为您提供行。它不会返回索引…同样,因为错误表明它需要整数,而条件是提供布尔值。为了有意义,在@SarthakNegi之后添加.index,我刚刚尝试了这个当前的\u index=my_df.iloc[my_df[current]您的目标是获得正确的索引?…您也可以发布错误消息,因为它对我来说很好…如果索引不是唯一标识符,您可以简单地使用np.where,它给出的输出带有警告:.ix已弃用。请使用.loc进行基于标签的索引,或者使用.iloc进行位置索引。在这种情况下,请使用.loc而不是.ix。谢谢。它确实有效。我想知道如何找到这两种方法的执行时间?我的意思是,哪一种是有效的和最好的代码?请让我来。@Msquare,您可以使用timeit,例如。我不希望您看到显著的性能差异;您应该首先检查这是否确实是应用程序中的瓶颈。
my_df.ix[my_df['Current']<0].index.values
df = pd.DataFrame({'Current': [1, 3, -4, 9, -3, 1, -2]})

res = df.iloc[df['Current'].lt(0).values].index

# Int64Index([2, 4, 6], dtype='int64')