我在python中有一个带有列的数据帧:
df
columnA
Apple Banana
Orange Citron Pineapple
如何根据空格保留子字符串的顺序?结果应该是:
columnA
Banana Apple
Pineapple Citron Orange
现在,我只使用:
df['columnA'] = df['columnA'].replace(r'(\s+).(\s+).(\s+)',r'\3\2\1',regex=True)
但这只在我知道子串的数目时有效,我不知道这个。你知道吗
Tags:
对于这个任务,我将使用列表理解,并避免使用
str
访问器您需要的三个步骤是:
第一步和第三步可以使用str.split和join实现,所以 你可以做:
输出
另一种选择是使用reversed:
相关问题 更多 >
编程相关推荐