![](/img/trans.png)
[英]Pandas: Remove all characters before a specific character in a dataframe column
[英]Pandas: Remove all characters before a specific character (last specific character) in a dataframe column that specific character is repeated 4 times
这是用 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 在删除最后一个“\”之后的所有字符后需要如下所示,请记住每个“反斜杠”前后都有一个空格
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
尝试拆分然后加入:
df['ParentPath'] = df['ParentPath'].str.split(' \\\\ ').str[:-1].str.join(' \\ ')
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.