Write a program in Python to replace all odd index position in a given series by random uppercase vowels

PythonPandasServer Side ProgrammingProgramming

Input − Assume, you have a Series,

0    1
1    2
2    3
3    4
4    5

Output

And, the result after replacing odd index with uppercase vowels as follows −

0    1
1    A
2    3
3    U
4    5

Solution

  • Define a Series.

  • Define uppercase alphabets

  • Create lambda filter method and replace vowels in all index positions. It is defined below

vowels = re.findall(r'[AEIOU]',chars)
result = pd.Series(filter(lambda x: r.choice(vowels) if(x%2!=0),l)data)

Example

import pandas as pd
import random as r
l = [1,2,3,4,5]
data = pd.Series(l)
print(“Given series:\n”, data)
vowels = list("AEIOU")
for i,j in data.items():
   if(i%2!=0):
      data[i]="".join(r.choice(vowels))
print("modified series:-\n",data)

Output

Given series:
0    1
1    2
2    3
3    4
4    5
dtype: int64
modified series:-
0    1
1    O
2    3
3    E
4    5
dtype: object
raja
Published on 10-Feb-2021 12:58:10
Advertisements