2016-01-05 77 views
1

鉴于这个数据帧:使用string.capwords与熊猫列

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 

我试图首字母大写只(不包括在“和的”大写的“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' 

在此先感谢!

回答

2

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

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和使用lambda与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 
+0

谢谢!如果我有这样的价值:“密歇根州 - 俄亥俄州”,我想让它说“密歇根州 - 俄亥俄州”? –

+1

你可以使用我的第一个答案,但分裂在'-'和覆盖,但你需要先遮蔽包含连字符的行 – EdChum