将数据框列中每个单词的首字母大写

Posted

技术标签:

【中文标题】将数据框列中每个单词的首字母大写【英文标题】:Capitalize first letter of each word in a dataframe column 【发布时间】:2017-01-01 16:24:33 【问题描述】:

如何将列中每个单词的首字母大写?顺便说一句,我正在使用 python pandas。例如,

         Column1
         The apple
         the Pear
         Green tea

我想要的结果是:

         Column1
         The Apple
         The Pear
         Green Tea

【问题讨论】:

【参考方案1】:

你可以使用str.title:

df.Column1 = df.Column1.str.title()
print(df.Column1)
0    The Apple
1     The Pear
2    Green Tea
Name: Column1, dtype: object

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

df.Column1 = df.Column1.str.capitalize()
print(df.Column1)
0    The apple
1     The pear
2    Green tea
Name: Column1, dtype: object

【讨论】:

以上是关于将数据框列中每个单词的首字母大写的主要内容,如果未能解决你的问题,请参考以下文章