简体   繁体   English

熊猫-在字符串列中某个字符之后“剪切”所有内容并将其粘贴到列的开头

[英]Pandas - 'cut' everything after a certain character in a string column and paste it in the beginning of the column

In a pandas dataframe string column, I want to grab everything after a certain character and place it in the beginning of the column while stripping the character. 在pandas dataframe字符串列中,我要抓住某个字符后的所有内容,并将其放在该列的开头,同时剥离该字符。 What is the most efficient way to do this / clean way to do achieve this? 什么是最有效的方法?

Input Dataframe: 输入数据框:

>>> df = pd.DataFrame({'city':['Bristol, City of', 'Newcastle, City of', 'London']})
>>> df
                 city
0    Bristol, City of
1  Newcastle, City of
2              London
>>>

My desired dataframe output: 我想要的数据帧输出:

                city
0    City of Bristol
1  City of Newcastle
2             London

Assuming there are only two pieces to each string at most, you can split, reverse, and join: 假设每个字符串最多只有两段,则可以拆分,反转和合并:

df.city.str.split(', ').str[::-1].str.join(' ')

0      City of Bristol
1    City of Newcastle
2               London
Name: city, dtype: object

If there are more than two commas, split on the first one only: 如果有两个以上的逗号,请仅对第一个进行拆分:

df.city.str.split(', ', 1).str[::-1].str.join(' ')

0      City of Bristol
1    City of Newcastle
2               London
Name: city, dtype: object

Another option is str.partition : 另一个选择是str.partition

u = df.city.str.partition(', ')
u.iloc[:,-1] + ' ' + u.iloc[:,0]

0      City of Bristol
1    City of Newcastle
2               London
dtype: object

This always splits on the first comma only. 这总是只在第一个逗号上分开。


You can also use a list comprehension, if you need performance: 如果需要性能,还可以使用列表推导:

df.assign(city=[' '.join(s.split(', ', 1)[::-1]) for s in df['city']])

                city
0    City of Bristol
1  City of Newcastle
2             London

Why should you care about loopy solutions? 您为什么要关心循环式解决方案? For loops are fast when working with string/regex functions (faster than pandas, at least). 使用字符串/正则表达式函数时,for循环很快(至少比pandas更快)。 You can read more at For loops with pandas - When should I care? 您可以在For循环与熊猫上阅读更多内容-我什么时候应该关心? .

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

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