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

Change case and reorder string fields in pandas dataframe

I have created the following dataframe:

import pandas as pd
ds1 = {'Name':["CARO QUINTERO, Miguel Angel","CHANG, Ping Yun","GILBOA, Joseph"]}

df1 = pd.DataFrame(data=ds1)

Which looks like this:

                          Name
0  CARO QUINTERO, Miguel Angel
1              CHANG, Ping Yun
2               GILBOA, Joseph

So, the text before the comma is the "last name", whilst the text after the comma is the first name (s).

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

I need to create a new column (called matchKey) such that:

  • it contains first and last name (in that order)
  • the first letter of the first name and last name is in upper case.

So, from the example above, the resulting dataframe would look like this:

[![enter image description here][1]][1]

Does anybody know how to do to it in pandas?
[1]: https://i.stack.imgur.com/By1Uz.png

>Solution :

Use:

df1['matchKey']=df1['Name'].str.replace(r'^(.*),\s*(.*)$',r'\2 \1', regex=True).str.title()
print (df1)
                          Name                    matchKey
0  CARO QUINTERO, Miguel Angel  Miguel Angel Caro Quintero
1              CHANG, Ping Yun              Ping Yun Chang
2               GILBOA, Joseph               Joseph Gilboa

Or:

s = df1['Name'].str.extract(r'^(.*),(.*)$')
df1['matchKey'] = s[1].str.cat( s[0].str.title(), sep=' ')
print (df1)
                          Name                     matchKey
0  CARO QUINTERO, Miguel Angel   Miguel Angel Caro Quintero
1              CHANG, Ping Yun               Ping Yun Chang
2               GILBOA, Joseph                Joseph Gilboa
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