Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reverse order of substring in pandas column

I have a pandas dataframe of that records the publications and the authors.

The dataframe is like this:

Title Author
A     A Ala, D Pamucar, EB Tirkolaee
B     A Heydari, S Niroomand
C     F Marisa, SS Syed Ahmad, N Kausar, S Kousar
...

I would like to reverse the order of the authors’ last names and the first names, so the last name will be listed first:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Title Author
A     Ala A, Pamucar D, Tirkolaee EB 
B     Heydari A, Niroomand S 
C     Marisa F, Syed Ahmad SS, Kausar N , Kousar S 
...

I’m thinking of using str.split to split the authors, and then use join and reversed. But the authors’ orders are changed too. Is there a better solution to do this?

>Solution :

You can use a regex. Assuming the first name has up to two letters here, but you can adapt if needed (use \w+ in place of \w{,2}):

df['Author'] = df['Author'].str.replace(r'\b(\w{,2})\b\s+\b([^,]+)\b',
                                        r'\2 \1', regex=True

output (as new columns "Author2" for clarity):

  Title                                       Author                                      Author2
0     A               A Ala, D Pamucar, EB Tirkolaee               Ala A, Pamucar D, Tirkolaee EB
1     B                       A Heydari, S Niroomand                       Heydari A, Niroomand S
2     C  F Marisa, SS Syed Ahmad, N Kausar, S Kousar  Marisa F, Syed Ahmad SS, Kausar N, Kousar S

regex:

\b(\w{,2})\b   # match first name (up to 2 letters)
\s+            # one or more spaces
\b([^,]+)\b    # one or more non "," characters
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading