Capitalize First Character of Elements in a Series

In this thread, you’ll get to know about different and easy methods of converting the first character of each element in a series into an uppercase character. The methods are discussed with a simple example code and are as follows:

1. Using "str.title()" method:

  • The str.title() method capitalizes the first character of each word in a string.

2. Using "str.capatilize()" method:

  • The str.capitalize() method also capitalizes the first character of a string.

3. Using "apply()" with lambda method:

  • In this technique, we first define a lambda function that applies the str.upper() method to the first character of a string.
  • Then we use the apply() method to apply the lambda function to each element in the series.

4. Using "str[0].str.upper()" method:

  • First, we use the str[0].str.upper() syntax to select the first character of each element in the series, and convert it to uppercase.
  • Then, we concatenate it with the rest of the string using the str.slice() method by giving an argument of start = 1 to get a string of the word excluding the first character.

5. Using "map()" function:

  • The map() function in Pandas allows applying any function (built-in, lambda, or custom function) to each element of a sequence of values, in our case, the sequence of values is a series.
  • A simple lambda function is applied to each element which capitalizes each element using capitalize().

6. Using list comprehension:

  • List comprehension is another way of applying a method to each element in a sequence of values and getting the result in the form of a list.
  • In this method also, the capitalize() method is applied to each element of the series to capitalize it.

Note: In the last two methods, if you want to obtain results in the form of a series, you can convert the list into a series using pd.Series() constructor.