简体   繁体   English

Pandas:删除特定字符重复4次的dataframe列中特定字符(最后一个特定字符)之前的所有字符

[英]Pandas: Remove all characters before a specific character (last specific character) in a dataframe column that specific character is repeated 4 times

Here is a dataframe constructed with a header column "ParentPath"这是用 header 列“ParentPath”构建的 dataframe

data = {'ParentPath': ['Hi \ All \ First Name \ Last Name \ A \ 200', 'Hi \ All \ First Name \ Middle Name \ Last Name \ B \ 33', 'Hi \ All \ First Name \ C \ 199', 'Hi \ All \ First Name \ D \ 333', 'Hi \ All \ First Name \ E \ 12', 'Hi \ All \ F \ 88']}
df = pd.DataFrame(data)

     ParentPath        
0   Hi \ All \ First Name \ Last Name \ A \ 200        
1   Hi \ All \ First Name \ Middle Name \ Last Name \ B \ 33        
2   Hi \ All \ First Name \ C \ 199        
3   Hi \ All \ First Name \ D \ 333        
4   Hi \ All \ First Name \ E \ 12        
5   Hi \ All \ F \ 88  

Output needed as shown below after removing all characters after the last " \ " keep in mind there is a space after and before each "backslash" Output 在删除最后一个“\”之后的所有字符后需要如下所示,请记住每个“反斜杠”前后都有一个空格

    ParentPath        
0   Hi \ All \ First Name \ Last Name \ A        
1   Hi \ All \ First Name \ Middle Name \ Last Name \ B        
2   Hi \ All \ First Name \ C        
3   Hi \ All \ First Name \ D        
4   Hi \ All \ First Name \ E        
5   Hi \ All \ F  

Try splitting and then joining:尝试拆分然后加入:

df['ParentPath'] = df['ParentPath'].str.split(' \\\\ ').str[:-1].str.join(' \\ ')

Output: Output:

                                          ParentPath
0                Hi \ All \ First Name \ Last Name \ A
1  Hi \ All \ First Name \ Middle Name \ Last Name \ B
2                            Hi \ All \ First Name \ C
3                            Hi \ All \ First Name \ D
4                            Hi \ All \ First Name \ E
5                                         Hi \ All \ F

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM