Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 3.x 我想删除没有特定值的行';不要增加。有没有更快/更优雅的方法?_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 我想删除没有特定值的行';不要增加。有没有更快/更优雅的方法?

Python 3.x 我想删除没有特定值的行';不要增加。有没有更快/更优雅的方法?,python-3.x,pandas,Python 3.x,Pandas,我有一个数据框,包含30列,1.000.000行,大小约为150MB。一列是分类的,包含7个不同的元素,另一列(Depth)主要包含越来越多的数字。每个元素的图形大致如下所示 我试图将列Depth保存为系列,并在删除不符合条件的行时对其进行迭代。这真是太慢了。 之后,我在数据框中添加了一个布尔列,指示是否删除它,因此我可以在一个步骤中删除最后的行。还是慢。我最后一次尝试(本文中的代码)是创建一个布尔列表,以保存通过条件的事实。仍然很慢(大约5小时) 当然,这应该适用于数据帧中的整行 有没有办法让

我有一个数据框,包含
30列
1.000.000行
,大小约为
150MB。一列是分类的,包含7个不同的元素,另一列(
Depth
)主要包含越来越多的数字。每个元素的图形大致如下所示

我试图将列
Depth
保存为系列,并在删除不符合条件的行时对其进行迭代。这真是太慢了。 之后,我在数据框中添加了一个布尔列,指示是否删除它,因此我可以在一个步骤中删除最后的行。还是慢。我最后一次尝试(本文中的代码)是创建一个布尔列表,以保存通过条件的事实。仍然很慢(大约5小时)

当然,这应该适用于数据帧中的整行

有没有办法让这更快

编辑: 输入数据帧的整行应保持原样。只有那些“深度”没有增加的部分应该被删除

编辑2:
其余行应保持其初始顺序。

采用两步方法如何。首先使用快速排序算法(例如快速排序),然后去除所有重复项?

好的,我找到了一种更快的方法。代码如下:

    dropList = [True]*len(df.index)
    for element in elements:
        currentMax = 0
        minIdx = df.loc[df['Element']==element]['Tiefe'].index.min()
        # maxIdx = df.loc[df['Element']==element]['Tiefe'].index.max()
        elementList = df.loc[df['Element']==element]['Tiefe'].to_list()

        for x in tqdm(range(len(elementList))):
            if elementList[x] < currentMax:
                dropList[x+minIdx]=False
            else:
                currentMax = elementList[x]
dropList=[True]*len(df.index)
对于元素中的元素:
currentMax=0
minIdx=df.loc[df['Element']==Element]['Tiefe'].index.min()
#maxIdx=df.loc[df['Element']==Element]['Tiefe'].index.max()
elementList=df.loc[df['Element']==Element]['Tiefe'].to_list()
对于tqdm(范围(len(elementList))中的x:
如果元素列表[x]
我获取了该列并将其保存为列表。为了保留,我保存的数据帧的索引是最低的,在循环中它会被再次添加


总的来说,问题似乎出在
loc
功能上。从最初的5小时运行时间开始,现在大约是10秒。

能否再添加一列以及两列的预期输出?好主意,但不幸的是,其余行应保持初始顺序。我忘了在问题中提到这一点。
Input:         'Depth' = [0 1 2 3 4 2 3 5 6]
      'AnyOtherColumn' = [a b c d e f g h i]

Output:            'Depth' [0 1 2 3 4 5 6]
        'AnyOtherColumn' = [a b c d e h i]
    dropList = [True]*len(df.index)
    for element in elements:
        currentMax = 0
        minIdx = df.loc[df['Element']==element]['Tiefe'].index.min()
        # maxIdx = df.loc[df['Element']==element]['Tiefe'].index.max()
        elementList = df.loc[df['Element']==element]['Tiefe'].to_list()

        for x in tqdm(range(len(elementList))):
            if elementList[x] < currentMax:
                dropList[x+minIdx]=False
            else:
                currentMax = elementList[x]