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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39141856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:53:32  来源:igfitidea点击:

Capitalize first letter of each word in the column Python

pythonstringpandasdataframecapitalization

提问by Jason Ching Yuk

how do you capitalize the first letter of each word in the column? I am using python pandas by the way. For example,

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

         Column1
         The apple
         the Pear
         Green tea

My desire result will be:

我的愿望结果将是:

         Column1
         The Apple
         The Pear
         Green Tea

回答by jezrael

You can use str.title:

您可以使用str.title

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

Another very similar method is str.capitalize, but it uppercases only first letters:

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

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