Convert the first and last character to upper case using Pandas

In this python pandas program, we will convert the first and last character to upper case using Pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Convert the first and last character to upper case using df.map(lambda x: x[0].upper() + x[1:-1] + x[-1].upper()).
  4. Pass each word of the series to the lambda function using df.map().
  5. Now using indexing and upper() convert the first and last character to upper case.
  6. Print the output.
				
					import pandas as pd
df = pd.Series(['sqatools','python','data','science'])
print("Original Series:")
print(df)
result = df.map(lambda x: x[0].upper() + x[1:-1] + x[-1].upper())
print("First and last character of each word to upper case:")
print(result)
				
			

Output :

				
					Original Series:
0    sqatools
1      python
2        data
3     science
dtype: object
First and last character of each word to upper case:
0    SqatoolS
1      PythoN
2        DatA
3     SciencE
dtype: object
				
			

extract items at given positions of a series

calculate the number of characters in each word in a series

Leave a Comment