简体   繁体   English

PANDAS - 根据最后一次出现的字符删除字符串的一部分

[英]PANDAS - remove a part of a string based on the last occurrence of a character

I have a data-frame:我有一个数据框:

df1 = pd.DataFrame({'Item': ["SYD_QANTAS AIRWAYS : LTD_DOC-Turn Cost :Sep", "SYD_QANTAS AIRWAYS LTD_DOC-Turn Cost :Jul", "SYD_QANTAS AIRWAYS LTD_DOC-Turn Cost :Aug"]})

I would like to remove the part of a string starting from the last occurrence of the character ":".我想从字符“:”的最后一次出现开始删除字符串的一部分。 This character can be present in the middle of the string as well but I want to remove the string only from the last occurrence, so the expected result would be:这个字符也可以出现在字符串的中间,但我只想从最后一次出现的地方删除字符串,所以预期的结果是:

在此处输入图像描述

How do I do that?我怎么做?

First we can split the string and join the list of strings excluding last entry首先,我们可以拆分字符串并加入字符串列表,不包括最后一个条目

you can try something like this你可以试试这样的

df1['Item']=df1['Item'].apply(lambda x:':'.join(x.split(':')[:-1]))

and the expected result would be:预期的结果是:

0 SYD_QANTAS AIRWAYS : LTD_DOC-Turn Cost
1      SYD_QANTAS AIRWAYS LTD_DOC-Turn Cost
2      SYD_QANTAS AIRWAYS LTD_DOC-Turn Cost

removes from reversed list until first ":"从反向列表中删除,直到第一个“:”

import pandas as pd

df1 = pd.DataFrame({'Item': ["SYD_QANTAS AIRWAYS : LTD_DOC-Turn Cost :Sep", 
                             "SYD_QANTAS AIRWAYS LTD_DOC-Turn Cost :Jul", 
                             "SYD_QANTAS AIRWAYS LTD_DOC-Turn Cost :Aug"]})

k = 0
for i in df1:
    while k < len(df1[i]):
        for j in list(reversed(df1[i][k])):
            if j==':':
                j_index = list(reversed(df1[i][k])).index(j)+1
                df1[i][k] = df1[i][k][:-j_index]
                break
        k +=1
print(df1)

outputs:输出:

                                      Item
0  SYD_QANTAS AIRWAYS : LTD_DOC-Turn Cost
1    SYD_QANTAS AIRWAYS LTD_DOC-Turn Cost
2    SYD_QANTAS AIRWAYS LTD_DOC-Turn Cost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:有没有一种方法可以查找和删除字符串中字符的第一个和最后一个出现的位置? - Python: Is there a way to find and remove the first and last occurrence of a character in a string? Python - Pandas - 删除第一次出现的字符和修复字符串之间的内容 - Python - Pandas - Remove content between the first occurrence of a character and a fix string 熊猫:最后一次出现时分割字符串 - Pandas: Split string on last occurrence 如何在熊猫列中的特殊字符之前删除字符串的一部分? - How to remove part of string ahead of special character in a column in Pandas? 正则表达式删除除第一次出现和最后一次出现的特殊字符 - regex remove special character except first occurrence and last occurrence 如果字符串与 pandas 中的模式匹配,则删除列中字符串的最后一部分 - Remove last part of string in column if strings match pattern in pandas 提取字符串 pandas 的最后一部分 - Extract last part of the string pandas 根据最后一次出现的分隔符将字符串拆分为2 - split string in to 2 based on last occurrence of a separator 通过在python中另一个字符之前最后出现的字符来解析字符串 - parse string by the last occurrence of the character before another character in python 在熊猫中的特定字符后删除句子的一部分 - Remove part of a sentence after specific character in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM