Search code examples
python-3.xpandascapitalize

Use string.capwords with Pandas column


Given this data frame:

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

I am attempting to capitalize the first letter only (without capitalizing the "s" in "And's").

The desired result is as follows:

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

The trouble is, when I do this:

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

I keep getting this error:

---------------------------------------------------------------------------
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'

Thanks in advance!


Solution

  • You can use the vectorised str.split and then apply a lambda and join:

    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
    

    or call apply and use a lambda with string.capwords:

    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