简体   繁体   English

替换并重新排列包含相似单词模式的字符串值

[英]Replace and rearrange string value containing similar pattern of words

I am working in google collaboratory and I have a panda dataframe like below我在谷歌合作,我有一个熊猫 dataframe 如下

Company name            Address
Meditera, PT            Street 1
Ocean express, PT       Street 2

I want to change to be like below:我想改变如下:

Company name            Address
PT Meditera            Street 1
PT Ocean express       Street 2

The way I do now is using df['Company name'].str.replace('Meditera, PT','PT Meditera') .我现在的方法是使用df['Company name'].str.replace('Meditera, PT','PT Meditera') The data is growing each day and it will be so exhaustive to replace one by one.数据每天都在增长,要一一替换会非常详尽。 The pattern of the data is the same, I only need to rearrange ', PT' from behind to the front of company name.数据的格式是一样的,我只需要把公司名称后面的',PT'重新排列到前面。

Is there any suggestion how we can do this in smarter way so that I do not need to manually use str.replace() every day.有什么建议我们可以如何以更智能的方式做到这一点,这样我就不需要每天手动使用str.replace()了。

Thanks before之前谢谢

df['Company name'] = df['Company name'].str.split(', ').str[::-1].str.join(' ')

Output: Output:

>>> df
       Company name   Address
0       PT Meditera  Street 1
1  PT Ocean express  Street 2

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

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