Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize first letter of each word in a dataframe column

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

         Column1          The apple          the Pear          Green tea 

My desire result will be:

         Column1          The Apple          The Pear          Green Tea 
like image 709
Jason Ching Yuk Avatar asked Aug 25 '16 09:08

Jason Ching Yuk


People also ask

How do you capitalize the first letter of a column in Dataframe?

Applying capitalize() function We apply the str. capitalize() function to the above dataframe for the column named Day. As you can notice, the name of all the days are capitalized at the first letter.

How do you capitalize every word in pandas?

Pandas – Convert the first and last character of each word to upper case in a series. In python, if we wish to convert only the first character of every word to uppercase, we can use the capitalize() method. Or we can take just the first character of the string and change it to uppercase using the upper() method.

How do I make the first letter capital in pandas?

How do you capitalize the first letter of a string? The first letter of a string can be capitalized using the capitalize() function. This method returns a string with the first letter capitalized. If you are looking to capitalize the first letter of the entire string the title() function should be used.

How do you make the first letter of each word capital in python?

capwords() capwords() is a python function that converts the first letter of every word into uppercase and every other letter into lowercase. The function takes the string as the parameter value and then returns the string with the first letter capital as the desired output.


1 Answers

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 
like image 146
jezrael Avatar answered Sep 23 '22 23:09

jezrael