比较列表或数据框中的上一个值和下一个值

2024-10-04 11:22:42 发布

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

我是新来的,经过很多研究,还没能破解这个。你知道吗

我的列表有点像这样:

lister=["AB1","AB2","AB3","AB3-2","AB3-3","AB3-4","AB4","AB4-2","AB5"]

它是现有文件夹的列表,不能更改为更实用的文件夹。 我还将此列表与其他一些值一起作为一列。你知道吗

目标是让具有“-2”、“-3”、“-#”的元素只使用具有最大值的元素。这些“-#”值最多可达10。你知道吗

上述清单的结果是:

resulter=["AB1","AB2","AB3-4","AB4-2","AB5]

非常感谢你的帮助!你知道吗

更新:

约翰·兹温克的答案对名单有用。然而,当我尝试在熊猫数据帧上使用它时,它会给我错误。因此,重新审视我的问题可能更有帮助:

我的数据帧如下所示:

   COL1 COL2    COL3   COL4     COL5      COL6
0   1    77      AB1  0.609856  2.145556  2.115333                                                                                                                                    
1   2    77      AB2  0.603378  2.146333  2.125667                                                                                                                                    
2   3    77      AB3  0.600580  2.150667  2.135000                                                                                                                                   
3   4    89      AB1  0.609129  2.149056  2.097667                                                                                                                                  
4   5    89      AB2  0.604061  2.175333  2.142667                                                                                                                                  
5   6    89      AB3  0.606987  2.139944  2.107333                                                                                                                                  
6   7    89      AB4  0.603696  2.122000  2.102000                                                                                                                                 
7   8    94      AB1  0.606438  2.156444  2.142000                                                                                                                                  
8   9    94    AB1-2  0.611260  2.133556  2.095000                                                                                                                                    
9  10    94      AB2  0.596059  2.169056  2.137333 

在这种情况下,我的要求是基于COL3(AB1)的值删除第7行,因为第8行中存在AB1-2值。你知道吗

再次感谢!你知道吗


Tags: 数据答案文件夹元素目标列表col3名单
2条回答

这不是最好的答案,我认为它的性能很差,但是如果有人需要没有任何模块的纯python或者使用Cython(类型化变量),这可能会有帮助:

lister=["AB1","AB2","AB3","AB3-2","AB3-3","AB3-4","AB4","AB4-2","AB5"]

resulter = list()
i=0
while i< len(lister)-1:
    if '-' not in lister[i] and '-' not in lister[i+1]:
        resulter.append(lister[i])
    elif '-' not in lister[i] and '-' in lister[i+1]:
        j=i+1
        tmp = lister[j]
        while '-' in tmp and j<len(lister)-1 and lister[i][2] == lister[j+1][2]:
            j += 1
            tmp = lister[j]
        i=j
        resulter.append(tmp)
    i+=1
if lister[-1] not in resulter:
    resulter.append(lister[-1])
print(resulter)
gb = pd.Series(lister).str.split('-', 1, expand=True).groupby(0)[1].last().fillna('')

给你:

AB1     
AB2     
AB3    4
AB4    2
AB5     

然后:

gb.index + np.where(gb, '-' + gb, '')

给你:

['AB1', 'AB2', 'AB3-4', 'AB4-2', 'AB5']

相关问题 更多 >