Open In App

Pandas – Convert the first and last character of each word to upper case in a series

Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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


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 :




# 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


Output :


Last Updated : 10 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads