将 string.capwords 与 Pandas 列一起使用

Use string.capwords with Pandas column

给定这个数据框:

df = pd.DataFrame(
      {'A' : ['''And's one''', 'And two', 'and Three'],
       'B' : ['A', 'B', 'A']})
df

    A           B
0   And's one   A
1   And two     B
2   and Three   A

我试图只将第一个字母大写(不将 "And's" 中的 "s" 大写)。

想要的结果如下:

    A           B
0   And's One   A
1   And Two     B
2   And Three   A

麻烦的是,当我这样做时:

import string    
df['A']=string.capwords(df['A'])

我不断收到此错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-106-d429a8e7cc45> in <module>()
----> 1 df['A']=string.capwords(df['A'])

C:\Users\zvsy0717\AppData\Local\Continuum\Anaconda3\lib\string.py in capwords(s, sep)
     42 
     43     """
---> 44     return (sep or ' ').join(x.capitalize() for x in s.split(sep))
     45 
     46 

C:\Users\zvsy0717\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   2244                 return self[name]
   2245             raise AttributeError("'%s' object has no attribute '%s'" %
-> 2246                                  (type(self).__name__, name))
   2247 
   2248     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'split'

提前致谢!

您可以使用矢量化 str.split 然后 apply lambda 并加入:

In [132]:
df['A'].str.split().apply(lambda x: [el.capitalize() for el in x]).str.join(' ')

Out[132]:
0    And's One
1      And Two
2    And Three
dtype: object

或调用 apply 并使用带有 string.capwords 的 lambda:

In [136]:
import string
df['A'] = df['A'].apply(lambda x: string.capwords(x))
df

Out[136]:
           A  B
0  And's One  A
1    And Two  B
2  And Three  A

这是最短的方法:

df['A']=df['A'].str.title()