pythonstringpandasdataframecapitalization

Capitalize first letter of each word in a dataframe column


How do you capitalize the first letter of each word in a pandas dataframe column? For example, I am trying to make the following transformation.

  Column1          Column1
The apple        The Apple
 the Pear   ⟶    The Pear
Green tea        Green Tea

Solution

  • You can use str.title:

    df.Column1 = df.Column1.str.title()
    print(df.Column1)
    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:

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