更改pandas列中的名称以大写字母开头

时间:2019-07-14 18:22:21

标签: python string pandas text apply

背景

我有一个玩具df

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

                      'P_ID': [1,2,3,4], 
                      'P_Name' : ['SMITH, JON J', 'RIDER, MARY LISA', 'DOE, JANE A', 'TUCKER, 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 Smith is Here         A1  1   SMITH, JON J
1   Mary Lisa Rider found here  A2  2   RIDER, MARY LISA
2   Jane A Doe is also here     A3  3   DOE, JANE A
3   Tom T Tucker is here to     A4  4   TUCKER, TOM T

目标

1)将P_Name的{​​{1}}列更改为类似于我期望的输出的格式;也就是说,将当前格式(例如df)更改为格式(例如SMITH, JON J),其中名字和姓氏和中间字母都以大写字母开头

2)在新列Smith, Jon J

中创建它

所需的输出

P_Name_New

问题

我如何实现理想的目标?

1 个答案:

答案 0 :(得分:2)

只需使用str.title()函数:

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