0

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. 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
noypikobe24
  • 105
  • 8

1 Answers1

2

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:

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). You can read more at For loops with pandas - When should I care?.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • 1
    You could use the `str.cat` with your `str.partition` solution - `u.iloc[:,-1].str.cat(u.iloc[:,0], sep=' ')`. Another approach is `apply`: `df.city.apply(lambda x: ' '.join(reversed(x.split(', '))))` - keep 'em coming :) – AChampion Jan 31 '19 at 02:49
  • 1
    thank you! I appreciate the detailed response along with the explanation of each – noypikobe24 Jan 31 '19 at 04:12