【问题标题】: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

【问题讨论】:

    标签: python string pandas dataframe capitalization


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2015-11-10
      • 2020-08-30
      • 2015-11-04
      • 2012-11-11
      • 2012-07-24
      • 2016-05-01
      • 1970-01-01
      • 2016-12-05
      • 2017-08-02
      相关资源
      最近更新 更多