In this python pandas program, we will convert the first and last character to upper case using Pandas library.
Steps to solve the program
- Import pandas library as pd.
- Create a series using pd.Series().
- Convert the first and last character to upper case using df.map(lambda x: x[0].upper() + x[1:-1] + x[-1].upper()).
- Pass each word of the series to the lambda function using df.map().
- Now using indexing and upper() convert the first and last character to upper case.
- 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