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. So, to convert the first and last character of each word to upper case in a series we will be using a similar approach. First of all, let’s create a series in Pandas. 
 

Example : Let’s create a Pandas Series

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd 
import pandas as pd
  
# Create the series
series = pd.Series(['geeks', 'for', 'geeks',
                    'pandas', 'series'])
  
# Print the series
print("Series:")
series

chevron_right


Output :

Once we have created a series using Pandas, we will apply a lambda() function to the entire series using the map() function. The lambda function will take the first character using slicing, capitalize it and add the rest of the string as it is until the last character. The last character is again capitalized and added to the resultant series.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Apply the lambda function to
# capitalize first and last 
# character to each word
newSeries = series.map(lambda x: x[0].upper() + x[1:-1] + x[-1].upper())
  
# Print the resulting series
print("\nResulting Series :")
newSeries

chevron_right


Output :

My Personal Notes arrow_drop_up

A Computer Professional aspiring to become one of the finest in the field

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.