Skip to content Skip to sidebar Skip to footer

Reversing Names In Pandas

I have a dataframe with a Name column like this: How can I use pandas to reverse the names in the format 'xxx, xxx' efficiently? Also if you have other string cleaning tips for mu

Solution 1:

Maybe you can try something like this with reverse function:

d = {'name':['Bran Stark','Jon Snow','Rhaegar Targaryen']}
df = pd.DataFrame(data=d)
df['new name'] = df['name'].apply(lambda x : ', '.join(reversed(x.split(' '))))
print(df['new name'])

0           Stark, Bran
1             Snow, Jon
2    Targaryen, Rhaegar

Solution 2:

Use Series.str.replace to perform regex string substitutions:

df['Name'] = df['Name'].str.replace(r'(.+),\s+(.+)', r'\2 \1')

The regex pattern (.+), (.+) means

(      begingroup #1
  .+match1-or-more ofanycharacter
)      endgroup #1
,      match a literal comma 
\s+match1-or-more whitespace characters
(      begingroup #2
  .+match1-or-more ofanycharacter
)      endgroup #2

The second argument r'\2 \1', tells str.replace to replace substrings that match the pattern with group #2 followed by a space, followed by group #1.


import pandas as pd
names = '''\
John Snow
Black, Jack
Jim Bean/
Draper, Don
'''
df = pd.DataFrame({'Name': names.splitlines()})
#           Name# 0    John Snow# 1  Black, Jack# 2    Jim Bean/# 3  Draper, Don

df['Name'] = df['Name'].str.replace(r'(.+),\s+(.+)', r'\2 \1')

yields

         Name
0   John Snow
1  Jack Black
2   Jim Bean/
3  Don Draper

Post a Comment for "Reversing Names In Pandas"