【问题标题】:Comparing previous and next values in a list or in a dataframe比较列表或数据框中的前一个值和下一个值
【发布时间】:2017-01-08 23:59:38
【问题描述】:

我是新来的,经过大量研究,无法破解这个。

我的列表看起来有点像这样:

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

它是现有文件夹的列表,不能更改为更实用的内容。 我也将此列表作为 pandas df 列以及其他一些值。

目标是对于具有“-2”、“-3”、“-#”的元素,只使用具有最大价值的元素。这些“-#”值最高可达 10。

上面列表的结果是:

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

非常感谢您的帮助!

更新:

John Zwinck 的答案适用于列表。但是,当我尝试在 pandas 数据框上使用它时,它给了我错误。因此,重新构建我的问题可能会更有帮助:

我的数据框如下所示:

   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 值。

再次感谢!

【问题讨论】:

    标签: python string list pandas


    【解决方案1】:
    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']
    

    【讨论】:

    • 非常感谢。奇迹般有效!现在开始在我的代码中实现...
    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2020-02-22
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多