将Python中的每个单词的首字母大写

Jas*_*Yuk 18 python string capitalization dataframe pandas

如何将列中每个单词的首字母大写?我顺便使用python pandas.例如,

         Column1
         The apple
         the Pear
         Green tea
Run Code Online (Sandbox Code Playgroud)

我的愿望结果将是:

         Column1
         The Apple
         The Pear
         Green Tea
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 34

你可以使用str.title:

print (df.Column1.str.title())
0    The Apple
1     The Pear
2    Green Tea
Name: Column1, dtype: object
Run Code Online (Sandbox Code Playgroud)

另一个非常相似的方法是str.capitalize,但它仅仅首字母大写:

print (df.Column1.str.capitalize())
0    The apple
1     The pear
2    Green tea
Name: Column1, dtype: object
Run Code Online (Sandbox Code Playgroud)