w3resource

Pandas Data Series: Convert the first and last character of each word to upper case

Pandas: Data Series Exercise-24 with Solution

Write a Pandas program convert the first and last character of each word to upper case in each word of a given series.

Sample Solution :

Python Code :

import pandas as pd
series1 = pd.Series(['php', 'python', 'java', 'c#'])
print("Original Series:")
print(series1)
result = series1.map(lambda x: x[0].upper() + x[1:-1] + x[-1].upper())
print("\nFirst and last character of each word to upper case:")
print(result)

Sample Output:

Original Series:
0       php
1    python
2      java
3        c#
dtype: object

First and last character of each word to upper case:
0       PhP
1    PythoN
2      JavA
3        C#
dtype: object

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to get the positions of items of a given series in another given series.
Next: Write a Pandas program to calculate the number of characters in each word in a given series.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Returns a list with n elements removed from the beginning

Example:

def tips_take(itr, n = 1):
  return itr[:n]
print(tips_take([1, 2, 3], 5))
print(tips_take([1, 2, 3], 0))

Output:

[1, 2, 3]
[]