How to capitalize the first character of elements in a Pandas Series?

I was working with a Pandas series containing all string names, and since these were names, I wanted to capitalize each one simultaneously using Python instead of manually going through each name. I used list comprehension to do this task, but are there more efficient ways of achieving this? Below, I am attaching the code I used:

Hi @mubashir_rizvi!

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

@mubashir_rizvi, You can also achieve your goal by using Python built-in functions for strings. You can use the str[0].str.upper() syntax to select the first character of each element in the series and convert it to uppercase, first. Then, you can 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.

Hey @mubashir_rizvi, you can use the map() function in Pandas that allows you to apply 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().