SFC
SFC

Reputation: 793

Change names in pandas column to start with uppercase letters

Background

I have a toy df

import pandas as pd
df = pd.DataFrame({'Text' : ['Jon J Mmith is Here', 
                                   'Mary Lisa Hder found here', 
                                   'Jane A Doe is also here',
                                'Tom T Tcker is here too'], 

                      'P_ID': [1,2,3,4], 
                      'P_Name' : ['MMITH, JON J', 'HDER, MARY LISA', 'DOE, JANE A', 'TCKER, TOM T'],
                      'N_ID' : ['A1', 'A2', 'A3', 'A4']

                     })

#rearrange columns
df = df[['Text','N_ID', 'P_ID', 'P_Name']]
df

                    Text      N_ID  P_ID    P_Name
0   Jon J Mmith is Here         A1  1   MMITH, JON J
1   Mary Lisa Hder found here   A2  2   HDER, MARY LISA
2   Jane A Doe is also here     A3  3   DOE, JANE A
3   Tom T Tcker is here to     A4   4   TCKER, TOM T

Goal

1) Change the P_Name column from df into a format that looks like my desired output; that is, change the current format (e.g.MMITH, JON J) to a format (e.g. Mmith, Jon J) where the first and last names and middle letter all start with a capital letter

2) Create this in a new column P_Name_New

Desired Output

                Text         N_ID P_ID    P_Name           P_Name_New
0   Jon J Mmith is Here         A1  1   MMITH, JON J     Mmith, Jon J
1   Mary Lisa Hder found here   A2  2   HDER, MARY LISA  Hder, Mary Lisa
2   Jane A Doe is also here     A3  3   DOE, JANE A      Doe, Jane A
3   Tom T Tcker is here too A4  4   TCKER, TOM T    Tcker, Tom T

Question

How do I achieve my desired goal?

Upvotes: 0

Views: 589

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Simply with str.title() function:

In [98]: df['P_Name_New'] = df['P_Name'].str.title()                                                                            

In [99]: df                                                                                                                     
Out[99]: 
                         Text N_ID  P_ID            P_Name        P_Name_New
0         Jon J Smith is Here   A1     1      SMITH, JON J      Smith, Jon J
1  Mary Lisa Rider found here   A2     2  RIDER, MARY LISA  Rider, Mary Lisa
2     Jane A Doe is also here   A3     3       DOE, JANE A       Doe, Jane A
3    Tom T Tucker is here too   A4     4     TUCKER, TOM T     Tucker, Tom T

Upvotes: 2

Related Questions